In my website, the user will be able to download multiples files in a ZIP. The problem is that zip->close()
delete all the files that I added in the foreach
loop.
Here is the code:
foreach ($file_name_list as $file_name) {
var_dump($zip);
echo '<br>';
$zip->addFile($files_path . $file_name, $file_name);
}
var_dump($zip);
echo '<br>';
$zip->close();
echo '<br>';
echo 'After closing:<br>';
var_dump($zip);
echo '<br>';
Here is the ouput:
object(ZipArchive)#4 (5) { ["status"]=> int(0) ["statusSys"]=> int(0) ["numFiles"]=> int(0) ["filename"]=> string(29) "/var/www/html/files/tests.zip" ["comment"]=> string(0) "" }
object(ZipArchive)#4 (5) { ["status"]=> int(0) ["statusSys"]=> int(0) ["numFiles"]=> int(1) ["filename"]=> string(29) "/var/www/html/files/tests.zip" ["comment"]=> string(0) "" }
object(ZipArchive)#4 (5) { ["status"]=> int(0) ["statusSys"]=> int(0) ["numFiles"]=> int(2) ["filename"]=> string(29) "/var/www/html/files/tests.zip" ["comment"]=> string(0) "" }
Afterclosing:
object(ZipArchive)#4 (5) { ["status"]=> int(0) ["statusSys"]=> int(0) ["numFiles"]=> int(0) ["filename"]=> string(0) "" ["comment"]=> string(0) "" }
As you can see, when PHP runs zip->close()
it deletes the files added. It also deletes the name of the file. I don't know what is wrong because it works fine on my Windows local machine but does not work on my Ubuntu virtual machine on the server.
How to fix that?
Do I really need zip->close()
? I just want to download the files not matter how.
I found the answer myself.
The problem was that when opening the zip through zip->open($zipname, ZipArchive::CREATE)
you have to use the full path to the zip, not the zip name. I think another problem is that the zip was created with few permissions, so it was not possible to download it.
The code that works:
function donwloadFilesInZip($array) {
$filesName = $array['files'];
$filesPath = rtrim($array['filesDir'], '/');
$zipName = $array['zipname'];
$zipPath = "$filesPath/$zipName";
$zip = new ZipArchive();
if($zip->open($zipPath, ZipArchive::CREATE)) {
foreach ($filesName as $fileName) {
if(file_exists("$filesPath/$fileName")) {
$zip->addFile("$filesPath/$fileName", $fileName);
}
}
$zip->close();
#file permissions
chmod($zipPath, 777);
#headers
header('Content-type: application/zip');
header("Content-Disposition: attachment; filename=$zipName");
header('Content-length: ' . filesize($zipPath));
#refresh the file permissions in order to read it
ob_clean();
flush();
#download
readfile($zipPath);
#delete
unlink($zipPath);
} else {
#display some error message here
}
}
The old code (worked well in my Windows local machine, but not in Ubuntu):
function donwload_files_in_zip($array) {
$file_name_list = $array['files_list'];
$zip_name = $array['zipname'];
$files_path = $array['files_path'];
$zip = new ZipArchive();
$zip->open($zip_name, ZipArchive::CREATE);
foreach ($file_name_list as $file_name) {
$zip->addFile($files_path . $file_name, $file_name);
}
$zip->close();
// headers
header('Content-Type: application/zip');
header("Content-disposition: attachment; filename = $zip_name");
header('Content-Length: ' . filesize($zip_name));
// download
readfile($zip_name);
// delete
unlink($zip_name);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.