<?php 

/* creates a compressed zip file */
function create_zip($files = array(), $destination = '', $overwrite = false, $prefix="")
{
    //if the zip file already exists and overwrite is false, return false
    if(file_exists($destination) && !$overwrite)
    {
    	return false;
    }

    //vars
    $valid_files = array();
    //if files were passed in...
    if(is_array($files))
    {
        //cycle through each file
        foreach($files as $file)
        {
            //make sure the file exists
            if(file_exists($file))
            {
                $valid_files[] = $file;
            }
        }
    }
    //if we have good files...
    if(count($valid_files))
    {
        //create the archive
        $zip = new ZipArchive();
        if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true)
        {
            return false;
        }
        //add the files
        foreach($valid_files as $file)
        {
        	if ($prefix)
			{
				$zip->addFile($file, str_replace($prefix, '', $file));
			}
			else
			{
				$zip->addFile($file, $file);
			}
        }
        //debug
        //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
        
        //close the zip -- done!
        $zip->close();
        
        //check to make sure the file exists
        return file_exists($destination);
    }
    else
    {
        return false;
    }
}

function directoryToArray($directory, $recursive, &$array_items, $exclude=null)
{
    if ($handle = opendir($directory))
    {
        while (false !== ($file = readdir($handle)))
        {
            if ($file != '.' && $file != '..' && $file != 'Thumbs.db' && $file != 'error_log')
            {
                if (is_dir($directory. '/' . $file))
                {
                	if ($exclude && in_array($directory. '/' . $file, $exclude))
                	{
                		continue;
                	}

                    if($recursive)
                    {
                        // $array_items = array_merge($array_items, directoryToArray($directory. '/' . $file, $recursive));
                        directoryToArray($directory . '/' . $file, $recursive, $array_items, $exclude);
                    }
                }
                else
                {
                    $file = $directory . '/' . $file;
                    $array_items[] = preg_replace('/\/\//si', '/', $file);
                }
            }
        }
        closedir($handle);
    }
}

function downloadFile($file)
{
	if (!file_exists($file) || !is_file($file))
	{
		return false;
	}

	try {
		header("Content-Disposition: attachment; filename=\"" . basename($file) . "\"");
		header("Content-Type: application/force-download");
		header("Content-Length: " . filesize($file));
		header("Connection: close");
		readfile($file);
		unlink($file);
	} catch(Exception $e) {
		header("Content-Disposition: attachment; filename=\"" . basename($file) . "\"");
		header("Content-Type: application/force-download");
		header("Content-Length: " . filesize($file));
		header("Connection: close");
		readfile($file);
		unlink($file);
	}
}
