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 />

我阅读了文档和一些论坛,发现这可能发生在以下情况之一:

  1. 如果没有实际的文件被添加到zip中,因为PHP 5.6——这可能是一个可能的解释,因为我最近升级到了PHP 5.6。但是:
    • 在添加之前,我会检查每个文件是否存在
    • 我尝试在zip中添加一个伪的非空文本文件。将其添加到zip将返回true,文件上的file_exists()也是如此
    • 当我回显$zip->numFiles并且它给出的数字至少为1时(当zip除了dummy之外没有文件时)
  2. 如果需要写入zip的目录不存在或没有正确的权限:情况似乎并非如此,但可以肯定的是,我在同一脚本中的同一文件夹中写入了一个文本文件,没有问题
  3. 如果写入临时目录时出现问题。检查这个有点困难,但我在同一个系统中有一个上传脚本,我确保没有磁盘空间问题等

这是相关代码。有些变量是预先定义的。请注意,我将每个问题都写入日志,并且此脚本不会生成任何条目!

$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)中被引用,在Jared在kippage dot.com:的一条评论中

这对一些人来说可能有点明显,但这是我的疏忽。

如果要将要删除的文件添加到zip文件中,请确保在调用close()函数后删除。

如果添加到对象的文件在保存时不可用,则不会创建zip文件。

(来源:https://www.php.net/manual/en/ziparchive.close.php#93322)

因此,根据我上面的代码;伪";文本文件在zip关闭之前被删除,这必然意味着在创建zip时该文件不存在。

我有充分的理由相信zip是在一个临时位置创建的,并且只移动到close()上的最终位置。事实并非如此。

因为这是谷歌中错误消息的第一位,我添加了另一个可能导致这个完全相同错误的问题。

如果您没有向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()之前添加了一些内容。

正如Ynhockey的回答中所提到的,最可能的原因是您在关闭zip文件之前删除了文件。一个显而易见的解决方案是在调用$zip->close()之后删除这些文件。但这里有一个替代方案。

而不是做

$zip->addFile('file.txt', 'file.txt');
unlink('file.txt');
$zip->close(); //gives error!

您可以调用addFromString函数并执行以下操作:

$zip->addFromString(file_get_contents('file.txt'), 'file.txt');
unlink('file.txt');
$zip->close(); //does not give any error!

检查行中"$full_thumb_path"的值

    $file_added = $zip->addFile($full_thumb_path, basename($item['thumb']));

该值应该是文件路径,而不能是目录路径。