Beware of the possible problem in the Adnan example: if the target file myarchive.zip is inside the source folder, you need to exclude it in a loop or run an iterator before creating the archive file (if it doesn't already exist). Here is a revised script that uses the latter , and adds some configuration vars at the top.This cannot be used to add to an existing archive.
<?php // Config Vars $sourcefolder = "./" ; // Default: "./" $zipfilename = "myarchive.zip"; // Default: "myarchive.zip" $timeout = 5000 ; // Default: 5000 // instantate an iterator (before creating the zip archive, just // in case the zip file is created inside the source folder) // and traverse the directory to get the file list. $dirlist = new RecursiveDirectoryIterator($sourcefolder); $filelist = new RecursiveIteratorIterator($dirlist); // set script timeout value ini_set('max_execution_time', $timeout); // instantate object $zip = new ZipArchive(); // create and open the archive if ($zip->open("$zipfilename", ZipArchive::CREATE) !== TRUE) { die ("Could not open archive"); } // add each file in the file list to the archive foreach ($filelist as $key=>$value) { $zip->addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key"); } // close the archive $zip->close(); echo "Archive ". $zipfilename . " created successfully."; // And provide download link ?> <a href="http:<?php echo $zipfilename;?>" target="_blank"> Download <?php echo $zipfilename?></a>
Potion 06 Sep '11 at 10:41 2011-09-06 10:41
source share