Zip-> close()尽管检查返回false
问题描述:
我试图用ZipArchive类创建一个zip文件。我在这里和那里阅读了很多,并实施了许多检查(文件存在,目录可写..)。但是zip-> close()仍然返回'false',我找不到原因。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;
}
}
我调用这个函数与
create_zip($files, 'PREFIX_'.$stamp, true);
其中$文件是一个数组,$邮票只是一个时间戳。 用于测试目标文件夹chmod设置为777
你能帮助我吗?
答
您在压缩操作中使用不正确的路径为您的文件是(也许):
在这里,你找../data/$file
:
if(file_exists('../data/'.$file) && is_readable('../data/'.$file)) {
$valid_files[] = $file;
但在荏苒的时候,你刚才:
$zip->addFile($file,$file);
代替
$zip->addFile("../data/$file",$file);
我看不出addFile如何能返回true,除非你凑巧碰巧有所有这些文件的副本在脚本的当前工作目录,或者你是在data
目录工作,../data
相同.
那些如果你在你的代码上盯着2个小时,事情就会发生。感谢唤醒电话。我应该由于愚蠢而删除我的问题吗? – Daniello
这取决于您,但删除往往会对您的帐户产生负面影响。 –