当我尝试在我的本地系统中使用以下代码下载zip文件时,该文件正在下载所有文件,但是在服务器中下载20字节,并且提取文件已损坏且未找到存档。所以我为这段代码添加了一些检查并运行但是这次没找到文件,进入这个检查(if(!is_file($ archive_file_name)))。服务器上可能出现什么问题?
function zipFilesAndDownload($file_names,$archive_file_name,$file_path)
{
$zip = new ZipArchive();
//create the file and throw the error if unsuccessful
if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {
exit("cannot open <$archive_file_name>\n");
}
//add each files of $file_name array to archive
foreach($file_path as $file_path)
{
if(basename($file_path) =="documents")
{
foreach($file_names as $value=>$files)
{
$customer_files = $this->fetchCustomFieldValuesAndFieldNamesByOrder($files['order_id']);
if($customer_files['field_value'] !="" && file_exists($file_path.$customer_files['field_value']))
{
$file_extension = pathinfo($customer_files['field_value'], PATHINFO_EXTENSION);
$zip_file_name = $files['customer_name']."_"."Files.".$file_extension;
$zip->addFile($file_path.$customer_files['field_value'],$zip_file_name);
}
//echo $customer_files['field_value']."<br>";
}
} else
{
foreach($file_names as $value=>$files)
{
if($files['file_name'] !="" && file_exists($file_path.$files['file_name']))
{
$file_extension = pathinfo($files['file_name'], PATHINFO_EXTENSION);
$zip_file_name = $files['customer_name']."_"."Application".".$file_extension";
$zip->addFile($file_path.$files['file_name'],$zip_file_name);
// echo $file_path.$files['file_name'],$zip_file_name."<br>";
}
}
}
}
// echo "numfiles: " . $zip->numFiles . "\n";
// echo "status:" . $zip->status . "\n";
$zip->close();//exit;
//then send the headers to foce download the zip file
if (headers_sent()) {
echo 'HTTP header already sent';
} else {
if (!is_file($archive_file_name)) {
header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found');
echo 'File not found';
} else if (!is_readable($archive_file_name)) {
header($_SERVER['SERVER_PROTOCOL'].' 403 Forbidden');
echo 'File not readable';
} else {
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=$archive_file_name");
header("Pragma: no-cache");
header("Expires: 0");
readfile("$archive_file_name");
exit;
}
}
}
答案 0 :(得分:2)
如果您从$ zip-&gt;状态获得状态11,则表示您尝试ZIP的文件不存在。以下是Zip / ZipArchive类中的错误代码文档:http://php.net/manual/en/zip.constants.php
使用getcwd()验证您所使用的路径是否正确,并且此路径上的文件确实存在。