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 フレームワークにあります。問題はストリーミングにあるようです。ダウンロードは一時フォルダーに正しく作成されます。そのZipをうまく解凍できます。ただし、ファイルがダウンロード用にストリーミングされると、拡張子 .cpgz のファイルが解凍されます。私が読んでいることから、これは Zip が破損していることを意味します。それをダブルクリックして解凍すると、拡張子が .cpgz の同じファイルの別のバージョンが取得され、それ自体が複製されます。
前にob_clean
andを追加しようとしましたが、役に立ちませんでした。ここで何が起こっているのですか?flush
readfile