br0ken Posted March 2, 2009 Share Posted March 2, 2009 I posted before about my system. It delivers an MP3 over HTTP. I was advised to include a content-length header to stop the connection timing out. I've done this but this has had no effect. I've read the following document: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.13 This says that the Content-Length field must be written in octets. Currently I use the filesize function on the actual mp3 to get the value for Content-Length. Should this be converted to octets in order for this to work? Quote Link to comment https://forums.phpfreaks.com/topic/147574-http-content-length/ Share on other sites More sharing options...
premiso Posted March 2, 2009 Share Posted March 2, 2009 Give it a try and see. If the standard says OCTETS, then I would use octets. Quote Link to comment https://forums.phpfreaks.com/topic/147574-http-content-length/#findComment-774719 Share on other sites More sharing options...
br0ken Posted March 2, 2009 Author Share Posted March 2, 2009 To do I just divide the the amount of bits by 8 to get the octet version? Quote Link to comment https://forums.phpfreaks.com/topic/147574-http-content-length/#findComment-774725 Share on other sites More sharing options...
premiso Posted March 2, 2009 Share Posted March 2, 2009 I doubt it really needs to be sent in octet, actually. For large files (100+ MBs), I found that it is essential to flush the file content ASAP, otherwise the download dialog doesn't show until a long time or never. <?php header("Content-Disposition: attachment; filename=" . urlencode($file)); header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-Type: application/download"); header("Content-Description: File Transfer"); header("Content-Length: " . filesize($file)); flush(); // this doesn't really matter. $fp = fopen($file, "r"); while (!feof($fp)) { echo fread($fp, 65536); flush(); // this is essential for large downloads } fclose($fp); ?> Found that at http://us2.php.net/manual/en/function.header.php#86554 Maybe that will solve your problem? Quote Link to comment https://forums.phpfreaks.com/topic/147574-http-content-length/#findComment-774729 Share on other sites More sharing options...
br0ken Posted March 2, 2009 Author Share Posted March 2, 2009 I've tried several combinations of header values, all of which fail. The download will download for a small amount of time but will eventually just stop. The download appears to be still downloading as it doesn't say 'download failed' but the progress bar stops going up. Here are two variations of my headers: header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: public"); header("Content-Description: File Transfer"); header("Content-Type: audio/mpeg"); header("Content-Disposition: attachment; filename=\"".basename($link)."\""); header("Content-Transfer-Encoding: binary"); header("Content-Length: ".$fsize); echo $buff; AND header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: public"); header("Content-Description: File Transfer"); header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-Type: application/download"); header("Content-Disposition: attachment; filename=\"".basename($link)."\""); header("Content-Length: ".$fsize); echo $buff; It should be noted that I've tried this on IE7 And Firefox 3 and get the same result for both. Quote Link to comment https://forums.phpfreaks.com/topic/147574-http-content-length/#findComment-774797 Share on other sites More sharing options...
premiso Posted March 2, 2009 Share Posted March 2, 2009 Did you set_time_limit for the script to be 0? Maybe it has something to do with your host? Are you on dedicated, shared or local? This could also have something to do with the PHP Memory Limit, if that is not greater than the size of the file, I think that would cause it to stop once that limit has been reached. (Most default to 16M). That can be changed via the ini_set or php.ini or .htaccess, whichever is available to you. Quote Link to comment https://forums.phpfreaks.com/topic/147574-http-content-length/#findComment-774806 Share on other sites More sharing options...
br0ken Posted March 2, 2009 Author Share Posted March 2, 2009 Yes, I've set the PHP time limit to 0 and set PHP's memory limit to 64M. I don't think this is the issue anyway as I don't get any errors indicating this. I don't know if this is important but I've noticed Firefox only ever downloads about 1mb where as IE7 downloads just over 2mb. Here is my download link if that would help: http://www.reflex-nutrition.com/reflex_radio_mp3.php?mp3id=1&mp3=48077226a1571e1f961fd2117e90c017&emailid=145&email=cf1f337fd6616d3a4789c56a74667899 Quote Link to comment https://forums.phpfreaks.com/topic/147574-http-content-length/#findComment-774815 Share on other sites More sharing options...
br0ken Posted March 2, 2009 Author Share Posted March 2, 2009 One more thing, which of my methods above is the correct way to force a file download? Should I send it as a audio/mpeg or octet stream or are either ways viable ways of achieving my goal? Quote Link to comment https://forums.phpfreaks.com/topic/147574-http-content-length/#findComment-774825 Share on other sites More sharing options...
br0ken Posted March 2, 2009 Author Share Posted March 2, 2009 Just to update everyone on my progress... I found some code that a few people claim works so I tried this. The code was based on the code presented on the following site: http://elouai.com/force-download.php Here is my actual code: <?php set_time_limit(0); ini_set('memory_limit', '64M'); header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: private", false); header("Content-Type: application/force-download"); header("Content-Disposition: attachment; filename=\"".basename($link)."\""); header("Content-Transfer-Encoding: binary"); header("Content-Length: ".$fsize); readfile($link); ?> This code makes the download run for slightly longer but still no more than 10% of the file size. The download pauses for a long time and then eventually produces the following error: I hope this new information can help someone figure out an answer for me! Thanks in advance! PS. The file I'm trying to download is 57mb Quote Link to comment https://forums.phpfreaks.com/topic/147574-http-content-length/#findComment-774947 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.