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
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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

no.. Haku is right, that tells the browser how many bytes to expect.

I think I might have misunderstood what he meant, then.  I took it to mean that a file that size is too big to work.  If that is not what was meant, sorry for misunderstanding!

Link to comment
Share on other sites

UPDATE:  Just made the changes (variable file size, rather than fixed number as well as set_time_limit(0) ).  The file is still failing between 103 and 105MB (file is 108.5MB).

 

Any additional ideas would be most appreciated!

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.