Reputation: 3560
I'm trying to create a ZIP archive of some image files. This directory of files, I have added to an array named $valid
. Each item in this array is a string like so:
"uploads/1845_45678_862_speaker.jpg"
This is my code that I'm trying to work, to create a ZIP archive of all of these physical files:
$zip = new ZipArchive();
$zip_name = "ptx_speakers.zip";
$zip->open($zip_name, ZipArchive::CREATE);
foreach ($valid as $zip_item) {
$zip->addFile($zip_item);
}
$zip->close();
However, this isn't working. Even with error reporting turned on, I just get a blank screen. I tried the following code afterwards, but nothing displayed here either:
if (!file_exists($zip_name)) {
"No file."
}
Can anyone see a fatal flaw in my code? I can paste more (like how I create the initial array) if you so wish.
Upvotes: 2
Views: 3740
Reputation: 7552
Can anyone see a fatal flaw in my code?
Yes.
$zip->open($zip_name, ZipArchive::CREATE);
is not working, and you are not checking for errors.
It should be something like
$res = $zip->open($zip_name, ZipArchive::CREATE);
if($res === true){
foreach ($valid as $zip_item) {
$zip->addFile($zip_item);
}
$zip->close();
} else {
echo "Error creating zip file: " . $res;
}
If everything works fine, $res
will be true, and move on to foreach, etc.
If not, it has one of these values. Check it, and you will see what is wrong.
In your code, you are doing
$zip->addFile($zip_item);
but the zip file is not created, what will cause some error.
Upvotes: 4
Reputation: 3560
Believe it or not, my problem lied not in my script, but in my file/folder permissions. Since I had permission to upload the files in the first place, I assumed I would be able to write with this file, but no, I had to update it's permissions to 757
and then the script worked a charm first time.
Upvotes: 1