我正试图修复一些图像的自动压缩脚本中的一个问题,这是我不久前写的,直到现在才开始工作。在$zip->close();之前,一切看起来都很好,它提供了以下内容:
<b>Warning</b>: ZipArchive::close(): Read error: No such file or directory in <b></b> on line <b>287</b><br />我阅读了文档和一些论坛,发现这种情况可能发生在以下情况之一:
file_exists()也是如此。$zip->numFiles并给出至少1(当压缩除了虚拟文件没有文件时)
这是相关代码。一些变量是预先定义的。请注意,我把每个问题都写到日志中,而这个脚本不会生成任何条目!
$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发布于 2016-05-24 12:58:46
事实证明,解决方案非常简单,实际上在docs (php.net)中,Jared在kippage网站上的评论中提到了这个问题:
对某些人来说,这似乎有点明显,但这是代表我的疏忽。 如果要将文件添加到要删除的zip文件,请确保在调用close()函数后删除。 如果添加到对象的文件在保存时不可用,则将不会创建zip文件。
(资料来源:https://www.php.net/manual/en/ziparchive.close.php#93322)
因此,从上面的代码中,在zip关闭之前删除了“虚拟”文本文件,这就必然使得在创建zip时该文件不存在。
我有充分的理由相信zip是在一个临时位置创建的,并且只移动到了close()上的最终位置。结果发现事实并非如此。
发布于 2021-04-13 19:12:09
仅仅因为这是Google中的第一条错误消息,我正在添加另一个可能的问题,导致这个完全相同的错误。
如果您没有向zip存档中添加任何文件,则它还不存在,因此关闭()将在空存档上失败。
例:
$zip = new ZipArchive;
$zip->open("foo.zip", ZipArchive::CREATE | ZipArchive::OVERWRITE);
$zip->close();生产:
ERROR: ZipArchive::close(): Can't remove file: No such file or directory因此,如果您正在循环和添加文件,请确保在调用close()之前添加了一些内容。
发布于 2016-07-25 01:47:24
检查行中"$full_thumb_path“的值
$file_added = $zip->addFile($full_thumb_path, basename($item['thumb']));该值应该是文件路径,而不能是目录路径。
https://stackoverflow.com/questions/37299433
复制相似问题