douan3182
douan3182
2016-08-11 02:06

PHP:ZipArchive :: close():重命名临时文件失败:没有这样的文件或目录

已采纳

I cannot create a zip file. It gives me this error. A search turned up no results. This is my function.

  function CompressFile($fromExtractedFile)
  {
      $archive = new ZipArchive;

      if ($archive->open($fromExtractedFile.".zip", ZipArchive::OVERWRITE | ZipArchive::CREATE) === TRUE) {
          $archive->addFile($fromExtractedFile, 'payload.csv');
          $archive->close();
      }
      else {
          throw new Exception ("Compress operation to ZIP file failed. $fromExtractedFile -> $fromExtractedFile.zip");
      }
      return $fromExtractedFile.".zip";
  }

Console:

Compressing /tmp/bing_PeUYiT
PHP Warning 'yii\base\ErrorException' with message 'ZipArchive::close(): Renaming temporary file failed: No such file or directory'

Docs: http://php.net/manual/en/book.zip.php

PHP 5.6, Win 8.1, Cygwin 2.5.2

Here is the same in PsySh:

>>> $archive = new ZipArchive
=> ZipArchive {#199
     +"status": 0,
     +"statusSys": 0,
     +"numFiles": 0,
     +"filename": "",
     +"comment": "",
   }
>>> $archive->open('/tmp/bing_PeUYiT.zip', ZipArchive::OVERWRITE|ZipArchive::CREATE)
=> true
>>> $archive->addFile('/tmp/bing_PeUYiT', 'payload.csv')
=> true
>>> $archive->close()
PHP warning:  ZipArchive::close(): Renaming temporary file failed: No such file or directory on line 1
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dpxo13079 dpxo13079 5年前

    Since ZipArchive is broken, I used PclZip instead.

    $ composer require pclzip/pclzip

      function compressFile2($file) {
        $zip = new PclZip($file.'.zip');
        $error = $zip->create([[PCLZIP_ATT_FILE_NAME => $file, PCLZIP_ATT_FILE_NEW_FULL_NAME => 'payload.csv']]); # array of array
        #$error = $zip->create($file, PCLZIP_OPT_REMOVE_ALL_PATH);
        if ($error == 0) trigger_error("Error : ".$zip->errorInfo(true));
        return $file.'.zip';
      }
    
    点赞 评论 复制链接分享