if(isset($_POST['items'])) {
    // open zip
    $zip_path = 'downloadSelected/download.zip';
    $zip = new ZipArchive();

    if ($zip->open($zip_path, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE) !== TRUE) {
       die ("An error occurred creating your ZIP file.");
     }

     foreach ($_POST['items'] as $path) {
        // generate filename to add to zip
        $filepath = 'downloads/' . $path . '.zip';
        if (file_exists($filepath)) {
                $zip->addFile($filepath, $path . '.zip') or die ("ERROR: Could not add the file $filename");
        }
        else {
                die("File $filepath doesnt exit");
                }
        $zip->close();
       } }

我收到警告:ZipArchive::addFile() [function.ZipArchive-addFile]:无效或统一的Zip对象。可能是什么问题?我尝试了许多方法,但徒劳无功。
选择一个文件后,我便可以创建并启动下载。但是,当我选择多个文件时,出现上述错误。

最佳答案

正确缩进的力量!

您正在循环中关闭zip文件。

如果我重新格式化您的代码,这很明显。

更正后的代码如下:

if(isset($_POST['items'])) {
  // open zip
  $zip_path = 'downloadSelected/download.zip';
  $zip = new ZipArchive();

  if ($zip->open($zip_path, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE) !== TRUE) {
    die ("An error occurred creating your ZIP file.");
  }

  foreach ($_POST['items'] as $path) {

    // generate filename to add to zip
    $filepath = 'downloads/' . $path . '.zip';

    if (file_exists($filepath)) {
      $zip->addFile($filepath, $path . '.zip') or die ("ERROR: Could not add the file $filename");
    } else {
      die("File $filepath doesnt exit");
    }
  }

  $zip->close();
}

关于php - 无效或单元化的Zip对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20029606/

10-10 03:35