MobileCushion
MobileCushion

Reputation: 7095

Yii - Downloaded zip file is corrupted

Downloading a zip file from Yii is resulting in corrupted files within the zip. I am able to open the zip, search its contents, but the data is corrupted. Opening the zip file in the file explorer shows that the zip file is OK. The problem only shows up when downloading a file:

$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);

I think the problem is with the $content argument to Yii's sendFile.

What should go in the content argument, so that the zip file is not corrupted?

Thanks

Upvotes: 0

Views: 826

Answers (1)

MobileCushion
MobileCushion

Reputation: 7095

This solved the problem

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; 

Upvotes: 1

Related Questions