Jump to content

Recommended Posts

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?

 

 

Link to comment
https://forums.phpfreaks.com/topic/147574-http-content-length/
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/147574-http-content-length/#findComment-774729
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/147574-http-content-length/#findComment-774797
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/147574-http-content-length/#findComment-774806
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/147574-http-content-length/#findComment-774815
Share on other sites

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:

 

 

print-screen-01.jpg

 

 

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

Link to comment
https://forums.phpfreaks.com/topic/147574-http-content-length/#findComment-774947
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.