Craig Hopson Posted April 20, 2012 Share Posted April 20, 2012 Hi I have found this script for zipping up directorys how would I zip all file EXCEPT 2, index.html and style.css Here's the code function Zip($source, $destination){ if (!extension_loaded('zip') || !file_exists($source)) { return false; } $zip = new ZipArchive(); if (!$zip->open($destination, 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)); } } } else if (is_file($source) === true) { $zip->addFromString(basename($source), file_get_contents($source)); } return $zip->close();} Thanks Code from here http://stackoverflow.com/questions/1334613/how-to-recursively-zip-a-directory-in-php Link to comment Share on other sites More sharing options...
justsomeguy Posted April 20, 2012 Share Posted April 20, 2012 Add an if statement inside the loop where it checks if the filename is a file, and make it check to make sure the filename is not one of the files you want to exclude. http://www.w3schools.com/php/php_if_else.asp You can have it print the filenames out in that loop if you want to see what they look like. Link to comment Share on other sites More sharing options...
Craig Hopson Posted April 20, 2012 Author Share Posted April 20, 2012 Sorry to be dim but where would I put the if statement??? Link to comment Share on other sites More sharing options...
birbal Posted April 21, 2012 Share Posted April 21, 2012 inside your foreach loop where you lopp throgh all the files Link to comment Share on other sites More sharing options...
Craig Hopson Posted April 21, 2012 Author Share Posted April 21, 2012 like this? foreach ($files as $file) { $file = str_replace('\\', '/', realpath($file)); if(!$file = 'index.php' || 'style.css'){ 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)); } } } Link to comment Share on other sites More sharing options...
Craig Hopson Posted April 21, 2012 Author Share Posted April 21, 2012 Actualy i have found this code that works better so same question differant script <?php//Get the directory to zip $filename_no_ext=$_GET['id']; // we deliver a zip file header("Content-Type: archive/zip"); // filename for the browser to save the zip file header("Content-Disposition: attachment; filename=$filename_no_ext".".zip"); // get a tmp name for the .zip $tmp_zip = tempnam ("tmp", "tempname") . ".zip"; //change directory so the zip file doesnt have a tree structure in it. chdir('uploads/'.$_GET['id']); // zip the stuff (dir and all in there) into the tmp_zip file exec('zip '.$tmp_zip.' *'); // calc the length of the zip. it is needed for the progress bar of the browser $filesize = filesize($tmp_zip); header("Content-Length: $filesize"); // deliver the zip file $fp = fopen("$tmp_zip","r"); echo fpassthru($fp); // clean up the tmp zip file unlink($tmp_zip);?> Link to comment Share on other sites More sharing options...
boen_robot Posted April 22, 2012 Share Posted April 22, 2012 Your second script won't work on most hosts, as most hosts disable exec(), or if they don't, they may not necessarily make "zip" available. If you only intend to run this file on a computer you control, fine, but beware.As for the question of excluding files... The second argument in the "zip" command is a pattern of all files to include. Either modify that pattern to only include the files you want OR check the help for the "zip" command, and see if it has any arguments allowing the exclusion of files. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now