我试图在一个自动zip脚本中解决一些问题,这是我刚才写的一些图片,直到现在才有效.一切似乎都很好,直到$zip-> close();它给出了以下内容:
Warning: ZipArchive::close(): Read error: No such file or directory in on line 287
我阅读了文档和一些论坛,发现这可能发生在以下某种情况中:
>如果没有实际文件添加到zip,自PHP 5.6以来,这可能是一个可能的解释,因为我最近升级到PHP 5.6.然而:
>我在添加之前检查每个文件是否存在
>我试图在zip中添加一个虚拟的非空文本文件.将其添加到zip将返回true,文件上的file_exists()也是如此
>当我回显$zip-> numFiles并且它给出一个至少为1的数字(当zip没有文件时除了假人)
>如果需要写入zip的目录不存在或者没有正确的权限:这似乎不是这种情况,但只是为了确定,我写了一个文本文件到同一个文件夹在同一个脚本中没有问题
>如果写入临时目录时出现问题.检查这个有点难,但我在同一系统中有一个上传脚本,我确保没有磁盘空间问题等.
这是相关的代码.一些变量是事先定义的.请注意,我将每个问题写入我的日志,并且此脚本不会生成任何条目!
$zip_file = 'Project'.$project_id.'.zip';
$zip = new ZipArchive;
if ($zip_result = $zip->open($zip_path.'/'.$zip_file, ZIPARCHIVE::CREATE) !== true) {
echo 'Error creating zip for project: '.$project_id.'. Error code: '.$zip_result;
help::debugLog('Error creating zip for project: '.$project_id.'. Error code: '.$zip_result);
return false;
}
$file_list = array();
foreach ($item_thumbs as $item)
{
$full_thumb_path = $thumb_dir.'/'.$item['thumb'];
if (file_exists($full_thumb_path) and $item['thumb'])
{
$file_added = $zip->addFile($full_thumb_path, basename($item['thumb']));
if (!$file_added)
help::debugLog('Failed to add item thumb to project zip. Project: '.$project_id.', file name: '.$item['thumb']);
else
$file_list[] = $item['thumb'];
}
elseif ($item['thumb']) /* If thumb indicated in DB doesn't exist in file system */
help::debugLog('Item thumb file '.$item['thumb'].' from item: '.$item['id'].' is missing from its indended location: '.$full_thumb_path);
}
/* Added 2016-05-18 -- creates dummy file for the zip listing its contents, important in case zip is empty */
$file_list_path = $zip_path.'/file_list.txt';
if (!($file_list_file = fopen($file_list_path, 'w+')))
help::debugLog('Failed to create list file (intended for zip) for project: '.$project_id);
fwrite($file_list_file, "File list:\n");
fwrite($file_list_file, implode("\n", $file_list));
if (file_exists($file_list_path))
{
fclose($file_list_file);
if (!$zip->addFile($file_list_path))
help::debugLog('Failed to add list file to project zip for project: '.$project_id);
unlink($file_list_path);
}
else
help::debugLog('Failed to create list file (intended for zip) for project: '.$project_id);
$zip->close(); // line 287