2014-11-05 182 views
0

從Yii下載zip文件導致壓縮文件中的文件損壞。我可以打開zip,搜索其內容,但數據已損壞。在文件資源管理器中打開壓縮文件顯示壓縮文件正常。下載文件時,該問題只顯示了:Yii - 下載的zip文件已損壞

$zip = new ZipArchive(); 
if($zip->open($zipname, ZIPARCHIVE::CREATE) === TRUE){ 

    // add stuff to zip 
    $res = $zip->addFile($a, $b); 
} 

Yii::app()->getRequest()->sendFile($zipname, file_get_contents($zipname), "application/zip", true); 

我認爲問題是出在$內容參數的Yii的SENDFILE。

內容參數應該怎麼做,以便壓縮文件沒有損壞?

感謝

+0

嘗試增加'$ zip->關閉();'您嘗試發送的文件,看看是否能解決這個問題之前。 – 2014-11-05 19:34:39

+0

你有任何調試或跟蹤運行(CWebLogRoute /或Yii調試工具欄等)?因爲這些會在請求結束時輸出內容,這會破壞一些文件類型。 – Stu 2014-11-06 10:06:36

+0

@WillemRenzema我有zip-> close(),只是忘了將它複製到問題中。 – MobileCushion 2014-11-06 10:14:13

回答

0

這解決了這個問題

header('Content-Description: File Transfer'); 
     header('Content-Type: application/zip'); 
     header('Content-Disposition: attachment; filename='.basename($zipname)); 
     header('Content-Transfer-Encoding: binary'); 
     header('Expires: 0'); 
     header('Cache-Control: must-revalidate'); 
     header('Pragma: public'); 
     header('Content-Length: ' . filesize($zipname)); 
     ob_clean(); 
     flush(); 
     readfile($zipname); 
     exit;