Jump to content

Help Needed Forcing Download Using PHP


dharvell

Recommended Posts

Hi all.  I am attempting to force a download of a ZIP file using PHP.  I have done this in the past, with no issue.  All of a sudden, I am having ALL sorts of issues.  For some reason, the download "finishes" about 3 MB before the file SHOULD finish, resulting in an incomplete file, therefore, a corrupt archive.  Please have a look at the code that I am using and see what I am doing incorrectly.  I am about 100% sure that it's in my code, somewhere...

 

$file = "INfinite.zip";

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: 113819246');
    ob_clean();
    flush();
    readfile($file);
    exit;

 

Any help with this would be MOST appreciated.  If my code looks correct, any pointers as to why this problem may exist would be appreciated, as well!

 

+dharvell

Link to comment
https://forums.phpfreaks.com/topic/161747-help-needed-forcing-download-using-php/
Share on other sites

for content length try filesize($file) instead of a static number.

 

could also be that the file is a bigger sized file and that would take longer that php's max timeout.. so to avoid this you could do:

 

set_time_limit(0)

 

at the top of the script

header('Content-Length: 113819246');

 

If your file is bigger than that, it's not going to work.

 

No necessarily true.  I had this working with a file that was 450MB in size.  Unless something changed server-side in the past 2 months, I would imagine that it should work for a file 108.5MB, as well.

for content length try filesize($file) instead of a static number.

 

could also be that the file is a bigger sized file and that would take longer that php's max timeout.. so to avoid this you could do:

 

set_time_limit(0)

 

at the top of the script

 

I forgot to mention that I did try to get the file size via variable.  When that didn't work, I attempted the static number, but forgot to change it back.  The set_time_limit(0), however, I have not tried, yet.  I'll give it a go and report back how it went.  Thanks for the reply!

Lets see your current code.

 

Current code is as follows:

 set_time_limit(0);

$file = "INfinite.zip";

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream'); /* have also tried application/zip */
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' .filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.