Reputation: 339
I'm trying to create a Zip file from files that are contained in a folder on my server.
Here's my code:
$zip = new ZipArchive();
$dirArray = array();
$new_zip_file = $temp_unzip_path.'test_new_zip.zip';
$new = $zip->open($new_zip_file, ZIPARCHIVE::CREATE);
if ($new === TRUE) {
$handle = opendir($temp_unzip_path);
while (false !== ($entry = readdir($handle))) {
$dirArray[] = $entry;
}
print_r ($dirArray);
closedir($handle);
} else {
echo 'Failed to create Zip';
}
$zip->close();
I'm really new to PHP so am trying to piece this together from the PHP manual along with some examples i've found on the net.
I have put the print_r
in there temporarily to make sure the entries are being added to the array - this is where i am planning to put the rest of the zip functions in future.
My problem is that it's not printing anything, so i don't know if the files are getting added to the zip or not.
My code is probably all wrong (i hope not) so if anyone can point me in the right direction i'd be very grateful.
Thanks.
Upvotes: 3
Views: 3378
Reputation: 3315
I've slightly modified your code and it creates a zip file without any problems.
If this isn't working for you check your permissions in the directory mydirectory
<pre><?
$temp_unzip_path = './mydirectory/';
$zip = new ZipArchive();
$dirArray = array();
$new_zip_file = $temp_unzip_path.'test_new_zip.zip';
$new = $zip->open($new_zip_file, ZIPARCHIVE::CREATE);
if ($new === true) {
$handle = opendir($temp_unzip_path);
while (false !== ($entry = readdir($handle))) {
if(!in_array($entry,array('.','..')))
{
$dirArray[] = $entry;
$zip->addFile($temp_unzip_path.$entry,$entry);
}
}
print_r ($dirArray);
closedir($handle);
} else {
echo 'Failed to create Zip';
}
$zip->close();
?>
Upvotes: 3