[英]Zip->close() returns false despite checks
I'm trying to create a zip file with the ZipArchive class. 我正在尝试使用ZipArchive类创建一个zip文件。 I was reading a lot here and there and implemented a lot of checks (file existence, directory writable..). 我在这里和那里读了很多书,并做了很多检查(文件存在,目录可写..)。 But zip->close() still returns 'false' and I can't figure out why. 但是zip-> close()仍然返回'false',我不知道为什么。
if (!is_writable('../temp_downloads/')) {
die('directory not writable'); }
function create_zip($files = array(),$destination = '',$overwrite = false) {
$destination = "../temp_downloads/".$destination.".zip";
if(file_exists($destination) && !$overwrite) { return false; }
$valid_files = array();
if(is_array($files)) {
foreach($files as $file) {
if(file_exists('../data/'.$file) && is_readable('../data/'.$file)) {
$valid_files[] = $file;
}
}
}
if(count($valid_files)) {
$zip = new ZipArchive();
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
exit('cannot create zip');
}
foreach($valid_files as $file) {
$zip->addFile($file,$file); // result returns 'true'
}
$res = $zip->close(); //$res contains 'false'
if(file_exists($destination)){
error_log("zip exists ".$destination);
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=$destination");
header("Content-length: " . filesize($destination));
header("Pragma: no-cache");
header("Expires: 0");
readfile("$destination");
} else {
error_log("ERROR: Zip doesnt exist at ".$destination);
}
return file_exists($destination);
} else {
return false;
}
}
I call this function with 我称这个功能为
create_zip($files, 'PREFIX_'.$stamp, true);
where $files is an array and $stamp just a timestamp. 其中$ files是一个数组,而$ stamp是一个时间戳。 For testing the destination folders chmod is set to 777 为了测试目标文件夹,将chmod设置为777。
Could you help me? 你可以帮帮我吗?
You're (maybe?) using incorrect paths for your files in the zip operations: 您(也许?)在zip操作中使用了错误的文件路径:
Here you look for ../data/$file
: 在这里,您寻找../data/$file
:
if(file_exists('../data/'.$file) && is_readable('../data/'.$file)) {
$valid_files[] = $file;
But at zipping time, you have just: 但是在压缩时,您只有:
$zip->addFile($file,$file);
instead of 代替
$zip->addFile("../data/$file",$file);
I don't see how addFile could return true, unless you coincidentally happen to have duplicates of all those files in the script's current working directory, or you ARE working in a data
directory and ../data
is the same as .
我看不到addFile如何返回true,除非您碰巧碰巧在脚本的当前工作目录中具有所有这些文件的重复项,或者您正在data
目录中工作并且../data
与相同.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.