<?php $error = ""; $file_folder = "temp/"; // folder to load files if (extension_loaded('zip')) { // Checking ZIP extension is available $zip = new ZipArchive(); // Load zip library $zip_name = "images_".date("dmY") . ".zip"; // Zip name if ($zip -> open($zip_name, ZIPARCHIVE::CREATE) !== TRUE) { // Opening zip file to load files $error .= "* Sorry ZIP creation failed at this time"; } $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("../uploads/")); $files = iterator_to_array($iterator, true); foreach ($iterator as $file) { $zip -> addFile($file_folder . $file); // Adding files into zip } print_r($zip); //echo $zip_name; if (file_exists($file_folder.$zip_name)) { // push to download the zip header('Content-type: application/zip'); header('Content-Disposition: attachment; filename="' . $zip_name . '"'); readfile($zip_name); // remove zip file is exists in temp path unlink($zip_name); } else { echo "error"; } $zip -> close(); //} else // $error .= "* Please select file to zip "; } else $error .= "* You dont have ZIP extension"; ?>
I tried to output the $ zip object and got the following answer: ZipArchive object ([status] => 0 [statusSys] => 0 [numFiles] => 12 [filename] => / var / www / bigb / ajax / images_24-12- 2012.zip [comment] =>)
Status Zero explains that there were no errors when creating the zip (link: http://www.php.net/manual/en/zip.constants.php )
I mentioned posts: 1) Downloading all images from the same website directory 2) How to create a zip file using php and delete it after user uploads? 3) http://www.9lessons.info/2012/06/creating-zip-file-with-php.html
The third one helped me the most, and in the other 2 I got the status 23.
Problem: I can create a zip and load it without any problems, but when I open the zip, I do not see any files inside, but the zip has memory in mbs.
Please help me...
Update: Error 23 is due to print_r / echo and / or trying to overwrite the same file.
Update 2: the problem is resolved. This happened because of the path in RecursiveDirectoryIterator (ie./uploads/) after I moved the code to the main folder and changed the path to (uploads /), it all started as it should. Kudos !!!
source share