PHP download script for big files

Today i had a task at one customer where a directory of files needed to be downloaded using a PHP script. For one strange reason the script failed and the browser displayed a File not found error. I couldn’t for a while figure out what was happening. But after some test it was obvious that the file size was too big for the server (90 MB)  because with a smaller file size it worked.

So this was the original code:

 

header("Content-length:".filesize($filename));
header('Content-Type: application/zip'); // ZIP file
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="downloadpackage.zip"');
header('Content-Transfer-Encoding: binary');
readfile($filename);
exit();

After some digging i quickly found the solution,  it is :  ob_end_clean();

Here is the final code:

header("Content-length:".filesize($filename));
header('Content-Type: application/zip'); // ZIP file
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="downloadpackage.zip"');
header('Content-Transfer-Encoding: binary');
ob_end_clean();
readfile($filename);
exit();

Works like a charm with any file size

Share

No related posts.

Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.

Comments

No comments yet.

Leave a comment

(required)

(required)


Get Adobe Flash playerPlugin by wpburn.com wordpress themes