dharvell Posted June 11, 2009 Share Posted June 11, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/161747-help-needed-forcing-download-using-php/ Share on other sites More sharing options...
haku Posted June 11, 2009 Share Posted June 11, 2009 header('Content-Length: 113819246'); If your file is bigger than that, it's not going to work. Quote Link to comment https://forums.phpfreaks.com/topic/161747-help-needed-forcing-download-using-php/#findComment-853405 Share on other sites More sharing options...
RussellReal Posted June 11, 2009 Share Posted June 11, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/161747-help-needed-forcing-download-using-php/#findComment-853406 Share on other sites More sharing options...
dharvell Posted June 11, 2009 Author Share Posted June 11, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/161747-help-needed-forcing-download-using-php/#findComment-853409 Share on other sites More sharing options...
RussellReal Posted June 11, 2009 Share Posted June 11, 2009 no.. Haku is right, that tells the browser how many bytes to expect. Quote Link to comment https://forums.phpfreaks.com/topic/161747-help-needed-forcing-download-using-php/#findComment-853410 Share on other sites More sharing options...
dharvell Posted June 11, 2009 Author Share Posted June 11, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/161747-help-needed-forcing-download-using-php/#findComment-853412 Share on other sites More sharing options...
dharvell Posted June 11, 2009 Author Share Posted June 11, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/161747-help-needed-forcing-download-using-php/#findComment-853419 Share on other sites More sharing options...
haku Posted June 11, 2009 Share Posted June 11, 2009 Lets see your current code. Quote Link to comment https://forums.phpfreaks.com/topic/161747-help-needed-forcing-download-using-php/#findComment-853422 Share on other sites More sharing options...
dharvell Posted June 11, 2009 Author Share Posted June 11, 2009 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; } Quote Link to comment https://forums.phpfreaks.com/topic/161747-help-needed-forcing-download-using-php/#findComment-853423 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.