This is my first time trying to create a ZIP file in PHP.
What I do, my PHP will search for files in a specific directory, grab them all and save to a ZIP file. Then the zip file will send the file to the browser for download. I am very close, but I am stuck in a certain part.
Here is my code:
$zip = new ZipArchive(); if ($zip->open('test.zip', ZIPARCHIVE::CREATE) !== TRUE) { die ("Could not open archive"); } $myDirectory = opendir("../folder/plugins/".$id.""); while($entryName = readdir($myDirectory)) { $dirArray[] = $entryName; } closedir($myDirectory); $indexCount = count($dirArray); sort($dirArray); for($index=0; $index < $indexCount; $index++) { if (substr("$dirArray[$index]", 0, 1) != "."){ $file = "".$myDirectory."".$dirArray[$index].".zip"; $zip->addFile($file, $file) or die ("cant add file"); ; echo $dirArray[$index]; echo '</br>'; }} $zip->close()or die("cant close");
I am trying to close the "cannot close" error. Please help me here, I cannot find what I am doing wrong in my code. This is what it prints:
filename1.png filename2.png can't close
:)
source share