For a automated download page, I want to quickly be able to add files that I'd like to share to a directory files
so that a ZIP from all files in that folder will be created.
The directory looks as follows:
index.php
|- files
|- file1.jpg
|- file2.jpg
|- file3.jpg
|- etc ...
Now, I have the following code in my index.php
:
$dir = "files";
$dh = opendir($dir);
$zip = new ZipArchive;
$zip->open('file.zip', ZipArchive::CREATE);
while (false !== ($filename = readdir($dh))) {
if ($filename !== "." && $filename !== "..") {
$files[] = $filename;
}
}
foreach ($files as $file) {
$zip->addFile($file,basename($file));
}
$zip->close();
When I var_dump($files)
I get a list of files like "file1.jpg" etc, but when I var_dump($zip)
I get:
object(ZipArchive)#1 (5) { ["status"]=> int(0) ["statusSys"]=> int(0) ["numFiles"]=> int(0) ["filename"]=> string(0) "" ["comment"]=> string(0) "" }
Can anyone help me out where this goes wrong? All files have 644 access and directories 755, I do not think that might be the problem.
Thank you
UPDATE: I found the problem with adding the files to the ZIP, but now I cannot figure out how to download the created zip, since it gets a 404 when trying to access the created zip. Updated piece of code:
foreach ($files as $file) {
$zip->addFile($dir . '/' . $file);
}