Before marking this post as a duplicate, please note that I was already looking for an answer on SO, and the one I have found so far (listed below) was not exactly what I was looking for.
- How to [recursively] Pin a directory to PHP?
- using zipArchive addFile () will not add the image to the zip code
- ZipArchive - addFile will not work
These are just some of the ones I looked at.
My problem is this: I cannot use addFromString, I had to use addFile, this is a requirement of the task.
I already tried a couple of ways, here is my current iteration:
public function getZippedFiles($path) { $real_path = WEBROOT_PATH.$path; $files = new RecursiveIteratorIterator (new RecursiveDirectoryIterator($real_path), RecursiveIteratorIterator::LEAVES_ONLY);
When I try to open the resulting file, they tell me that "Windows cannot open the folder. The compressed (compressed) folder" "is invalid.
I was able to successfully complete the task using addFromString, for example:
$file_path = WEBROOT_PATH.$path; $files = array(); if (is_dir($file_path) == true) { if ($handle = opendir($file_path)) { while (($file = readdir($handle)) !== false) { if (is_dir($file_path.$file) == false) { $files[] = $file_path."\\".$file; } } //# create new zip opbject $zip = new ZipArchive(); //# create a temp file & open it $tmp_file = tempnam($file_path,''); $zip_file = preg_replace('"\.tmp$"', '.zip', $tmp_file); $zip->open($zip_file, ZipArchive::CREATE); //# loop through each file foreach($files as $file){ //# download file $download_file = file_get_contents($file); //#add it to the zip $zip->addFromString(basename($file),$download_file); } //# close zip $zip->close(); } } }
Above all, itβs just copied directly from some sample code that I saw somewhere. If someone can point me in a good direction, I would be very grateful!
***** UPDATE ***** I added that around: like this:
if (!$zip->close()) { echo "failed writing zip to archive"; }
The message gets an echo, so obviously the problem is there. I also checked to make sure $zip->open() working, and I confirmed that it opens it without problems.