首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ZipArchive关闭警告

ZipArchive关闭警告
EN

Stack Overflow用户
提问于 2016-05-18 12:19:04
回答 3查看 23.3K关注 0票数 18

我正试图修复一些图像的自动压缩脚本中的一个问题,这是我不久前写的,直到现在才开始工作。在$zip->close();之前,一切看起来都很好,它提供了以下内容:

代码语言:javascript
复制
 <b>Warning</b>:  ZipArchive::close(): Read error: No such file or directory in <b></b> on line <b>287</b><br />

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

  1. 如果没有实际的文件添加到zip中,因为PHP5.6-这可能是一个可能的解释,因为我最近升级到PHP5.6。然而:
    • 在添加文件之前,我检查每个文件是否存在。
    • 我试图向zip中添加一个虚拟的非空文本文件。将其添加到zip返回true,文件上的file_exists()也是如此。
    • 当我回显$zip->numFiles并给出至少1(当压缩除了虚拟文件没有文件时)

  1. 如果需要写入zip的目录不存在或没有正确的权限:情况似乎不是这样,但为了确定,我在同一个脚本中向同一个文件夹写入了一个文本文件,没有问题
  2. 如果有问题,写到临时目录。这有点困难,但我有一个上传脚本在同一个系统中工作,我确保没有磁盘空间问题等。

这是相关代码。一些变量是预先定义的。请注意,我把每个问题都写到日志中,而这个脚本不会生成任何条目!

代码语言:javascript
复制
$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
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 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()上的最终位置。结果发现事实并非如此。

票数 23
EN

Stack Overflow用户

发布于 2021-04-13 19:12:09

仅仅因为这是Google中的第一条错误消息,我正在添加另一个可能的问题,导致这个完全相同的错误。

如果您没有向zip存档中添加任何文件,则它还不存在,因此关闭()将在空存档上失败。

例:

代码语言:javascript
复制
$zip = new ZipArchive;
$zip->open("foo.zip", ZipArchive::CREATE | ZipArchive::OVERWRITE);
$zip->close();

生产:

代码语言:javascript
复制
ERROR: ZipArchive::close(): Can't remove file: No such file or directory

因此,如果您正在循环和添加文件,请确保在调用close()之前添加了一些内容。

票数 1
EN

Stack Overflow用户

发布于 2016-07-25 01:47:24

检查行中"$full_thumb_path“的值

代码语言:javascript
复制
    $file_added = $zip->addFile($full_thumb_path, basename($item['thumb']));

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

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37299433

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档