【发布时间】:2014-06-30 16:23:14
【问题描述】:
我正在尝试使用 ZipArchive 类创建一个 zip 文件。我在这里和那里阅读了很多内容并实施了很多检查(文件存在,目录可写..)。但是 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);
其中 $files 是一个数组,而 $stamp 只是一个时间戳。 为了测试目标文件夹 chmod 设置为 777
你能帮帮我吗?
【问题讨论】: