wzazza
wzazza

Reputation: 853

PHP: readfile() returns zip file with 0 bytes

I have created php script to allow user to download zip files. Script looks like:

$filePath = '/path/to/zipfile.zip'; // file is below /public_html/ directory

if(file_exists($filePath)) {
    $fileName = basename($filePath); 
    $fileSize = filesize($filePath); //returns: 26494938

    header("Content-Type: application/zip");
    header("Content-Length: ".$fileSize);
    header("Content-Disposition: attachment; filename=".$fileName);

    readfile($filePath);                            
    die;    
};

I would expect this script to pass zipfile.zip for downloading. And it does, the save dialog pops up, I am choosing where to save, pressing save and zip file saves. Except one important thing - the file is with 0 bytes, it is completely empty zip file. Original zip file is full with files (around 25Mb). Maybe the size is the problem?

Can someone please help with some advice? Path to file is correct. I suspect that the problem is most likely related to readfile() function and/or zip file size, if so is there some alternative to readfile() that would work? Thank you!

Update: I have tried to use function readfile_chunked as mentioned in this post: "Readfile reads 0 bytes from large file?" - the result is that zip after downloading now have some size (no longer 0 bytes) and all correct content inside it, looks like everything is ok, but the zip is invalid, and cannot be unzipped and opened normally. In Win8 when trying to open downloaded zip file I am receiving this error: The Compresed (zipped) Folder zipfile.zip is invalid.. Of course the original zip file works fine. WinRAR shows this error: zipfile.zip: Unexpected end of archive. It looks the same problem as described in this post, without any working solution.

Upvotes: 1

Views: 2838

Answers (2)

iam.dreamcode
iam.dreamcode

Reputation: 21

Try to add ob_end_flush(); before readfile($filePath); . It

Upvotes: 2

Mordax Praetorian
Mordax Praetorian

Reputation: 9

The simplest solution is to do away with the php completely, just provide an HTML link to the zip and let your server do the work

also if you really must stick with php to serve this, you have no code there that actually sends the file to the user, readfile doesn't do that it just loads it into the program

Upvotes: 0

Related Questions