ZipArchive 关闭警告
Warning on ZipArchive Close
我正在尝试修复一些图像的自动压缩脚本中的问题,这是我不久前编写的,并且一直有效到现在。一切似乎都很好,直到 $zip->close();
给出以下内容:
<b>Warning</b>: ZipArchive::close(): Read error: No such file or directory in <b></b> on line <b>287</b><br />
我阅读了文档和一些论坛,发现这可能发生在以下情况之一:
- 如果没有实际文件添加到 zip,因为 PHP 5.6 — 这可能是一个可能的解释,因为我最近升级到 PHP 5.6。然而:
- 我在添加之前检查每个文件是否存在
- 我试图向 zip 添加一个虚拟的非空文本文件。将其添加到 zip returns 是的,文件
file_exists()
也是如此
- 当我回显
$zip->numFiles
并且它给出的数字至少为 1(当 zip 除了虚拟文件之外没有文件时)
- 如果需要写入 zip 的目录不存在或没有正确的权限:这似乎不是这种情况,但为了确定,我写了一个文本文件到同一个脚本中的同一个文件夹,没有问题
- 如果写入临时目录时出现问题。检查这个有点难,但我在同一个系统中有一个上传脚本可以工作,我确保没有磁盘 space 问题等
这是相关代码。一些变量是预先定义的。请注意,我将每个问题都写到我的日志中,并且此脚本不会生成任何条目!
$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
事实证明解决方案非常简单,它实际上在文档 (php.net) 中被引用,在 kippage dot com 的 Jared 的评论之一中:
It may seem a little obvious to some but it was an oversight on my behalf.
If you are adding files to the zip file that you want to be deleted make sure you delete AFTER you call the close() function.
If the files added to the object aren't available at save time the zip file will not be created.
(来源:https://www.php.net/manual/en/ziparchive.close.php#93322)
因此,根据我上面的代码,“虚拟”文本文件在 zip 关闭之前被删除,这必然导致该文件在创建 zip 时不存在。
我有充分的理由相信该 zip 是在一个临时位置创建的,只是移动到 close()
的最终位置。事实证明并非如此。
检查行
中“$full_thumb_path”的值
$file_added = $zip->addFile($full_thumb_path, basename($item['thumb']));
该值应该是文件路径,不能是目录路径。
仅仅因为这是 Google 中错误消息的第一条,我添加了另一个可能导致此完全相同错误的问题。
如果您不向 zip 存档添加任何文件,则它还不存在,因此 close() 将在空存档上失败。
例如:
$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() 之前添加了一些内容。
我正在尝试修复一些图像的自动压缩脚本中的问题,这是我不久前编写的,并且一直有效到现在。一切似乎都很好,直到 $zip->close();
给出以下内容:
<b>Warning</b>: ZipArchive::close(): Read error: No such file or directory in <b></b> on line <b>287</b><br />
我阅读了文档和一些论坛,发现这可能发生在以下情况之一:
- 如果没有实际文件添加到 zip,因为 PHP 5.6 — 这可能是一个可能的解释,因为我最近升级到 PHP 5.6。然而:
- 我在添加之前检查每个文件是否存在
- 我试图向 zip 添加一个虚拟的非空文本文件。将其添加到 zip returns 是的,文件
file_exists()
也是如此 - 当我回显
$zip->numFiles
并且它给出的数字至少为 1(当 zip 除了虚拟文件之外没有文件时)
- 如果需要写入 zip 的目录不存在或没有正确的权限:这似乎不是这种情况,但为了确定,我写了一个文本文件到同一个脚本中的同一个文件夹,没有问题
- 如果写入临时目录时出现问题。检查这个有点难,但我在同一个系统中有一个上传脚本可以工作,我确保没有磁盘 space 问题等
这是相关代码。一些变量是预先定义的。请注意,我将每个问题都写到我的日志中,并且此脚本不会生成任何条目!
$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
事实证明解决方案非常简单,它实际上在文档 (php.net) 中被引用,在 kippage dot com 的 Jared 的评论之一中:
It may seem a little obvious to some but it was an oversight on my behalf.
If you are adding files to the zip file that you want to be deleted make sure you delete AFTER you call the close() function.
If the files added to the object aren't available at save time the zip file will not be created.
(来源:https://www.php.net/manual/en/ziparchive.close.php#93322)
因此,根据我上面的代码,“虚拟”文本文件在 zip 关闭之前被删除,这必然导致该文件在创建 zip 时不存在。
我有充分的理由相信该 zip 是在一个临时位置创建的,只是移动到 close()
的最终位置。事实证明并非如此。
检查行
中“$full_thumb_path”的值 $file_added = $zip->addFile($full_thumb_path, basename($item['thumb']));
该值应该是文件路径,不能是目录路径。
仅仅因为这是 Google 中错误消息的第一条,我添加了另一个可能导致此完全相同错误的问题。
如果您不向 zip 存档添加任何文件,则它还不存在,因此 close() 将在空存档上失败。
例如:
$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() 之前添加了一些内容。