如何防止Zip文件在流式传输时损坏

时间:2013-11-11 18:22:21

标签: php download zip

我遇到了使用PHP流式传输Zip文件以供下载的问题。这是我的代码:

$arrFiles = $this->getRequest()->getPost('files', array());
$objFacultyClubDownloads = new FacultyClub_Downloads();
$objFCDownloads = $objFacultyClubDownloads->saveFacultyClubDownloadsInfo($arrFiles);

// create the zip file

 $strZipName = 'download' . time() . '.zip';
 $strZipSavePath = PUBLIC_PATH . '/downloads/temp/';

 $objZip = new ZipArchive;

 if ($objZip->open($strZipSavePath . $strZipName, ZipArchive::CREATE) === TRUE) {

 foreach ($arrFiles['file_name'] as $intKey => $strValue) {
     $strFilePath = PUBLIC_PATH . $strValue;
     if (file_exists($strFilePath)) {
         $strFileName = strtolower(basename($strValue));
         $objZip->addFile($strFilePath, $strFileName);
      }
  }

   $strZipName = $objZip->filename;
   $objZip->close();
}
// strip out the layout and view

$this->getHelper('layout')->disableLayout();
$this->getHelper('viewRenderer')->setNoRender();

 // stream the zip file

 header('Content-Type: application/zip');
 header("Content-Disposition: attachment; filename=Faculty-Club-Download.zip");
 //header('Content-Length: ' . filesize($strZipName));
 readfile($strZipName);

这是在Zend Framework中。问题似乎是流媒体。在temp文件夹中正确创建了下载。我可以拉开拉链就好了。但是当文件流式传输下载时,它会解压缩扩展名为.cpgz的文件。从我正在阅读的内容来看,这意味着Zip已损坏。当我双击它解压缩它时,我只是使用.cpgz扩展名获得同一文件的另一个版本 - 它会复制自己。

我尝试在ob_clean之前添加flushreadfile,但这没有用。任何想法在这里发生了什么?

1 个答案:

答案 0 :(得分:1)

如果服务器上的文件有效,但在通过http传输损坏后,我会查看PHP和浏览器之间的所有内容。假设您正在使用Apache,请启动并禁用此请求的gzip。我们遇到了类似的反向代理和双重压缩问题。

可能必须在PHP代码中尽早调用它:

apache_setenv('no-gzip', '1');

http://php.net/apache_setenv