PHP zip 创建了损坏的 zip 文件

问题描述 投票:0回答:0

我在 MVC 框架中创建了 PHP 项目。在该项目中,用户可以添加多个图像并下载已由其他用户上传的图像 zip 文件。为此,我编写了以下代码,但它正在创建损坏的 zip 文件。我不明白这是什么问题。我已经调试并检查了图像和所有内容。

打开下载的 zip 时出现此错误:“存档格式未知或已损坏”

$userId = substr($_GET['url'], strrpos($_GET['url'], '/') + 1);
    $image_data = $this->ImageModel->getImagesById($userId);
    if(!empty($image_data)) {
      $images = json_decode(json_encode($image_data), true);
      $zip = new ZipArchive();
      $zip_name = "image.zip"; // Zip name
      $path = URLROOT.'/public/site_image/';
      //echo $path;die;
      //create the file and throw the error if unsuccessful
      if ($zip->open($zip_name, ZIPARCHIVE::CREATE )!==TRUE) {
          exit("cannot open <$zip_name>\n");
      }
      //add each files of $file_name array to archive
      foreach($images as $key => $image)
      {
        // image path
        //http://localhost/my-project/public/site_image/1678208758_688.jpg
       //http://localhost/my-project/public/site_image/images.jpg
        
        $zip->addFile($path.$image['image'],$image['image']);
      } 
      $zip->close();
      header("Content-type: application/zip"); 
      header("Content-Disposition: attachment; filename=$zip_name");
      header("Content-length: " . filesize($zip_name));
      header("Pragma: no-cache"); 
      header("Expires: 0"); 
      readfile("$zip_name");
php model-view-controller zip php-7.4
© www.soinside.com 2019 - 2024. All rights reserved.