Zip Directory Recursively

<?php

$file = tempnam("tmp","zip");
zip_directory('/home/mydirectorypath',$file);

header('Content-Type: application/zip');
header('Content-Length: ' . filesize($file));
header('Content-Disposition: attachment; filename="myzip.zip"');

readfile($file);
unlink($file);

function zip_directory($source,$tempfile){
if(!extension_loaded('zip') || !file_exists($source)) return false;
$zip = new ZipArchive();
if(!$zip->open($tempfile,ZIPARCHIVE::CREATE)) return false;
$source = str_replace('\','/',realpath($source));
if(is_dir($source) === true){
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
foreach($files as $file){
$file = str_replace('\', '/', realpath($file));
if(is_dir($file) === true) $zip->addEmptyDir(str_replace($source . '/','', $file . '/'));
else if(is_file($file) === true) $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
}
}
elseif(is_file($source) === true) $zip->addFromString(basename($source), file_get_contents($source));
return $zip->close();
}

?>

Temp Zip for Download

<?php

$file = tempnam("tmp","zip");
$zip = new ZipArchive();
$zip->open($file,ZipArchive::OVERWRITE);

//Throw an extra file in dynamically
$dynamic_content = 'This is the README file that comes with your downloaded zip. Waka Waka';
$zip->addFromString('readme.txt', $dynamic_content);

//Include files in archive
$zip->addFile('testfile.txt', 'testfile.txt');
$zip->addFile('testfile2.txt', 'testfile2.txt');

$zip->close();

header('Content-Type: application/zip');
header('Content-Length: ' . filesize($file));
header('Content-Disposition: attachment; filename="myzippedfiles.zip"');

readfile($file);
unlink($file);

?>