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.