By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,230 Members | 2,427 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,230 IT Pros & Developers. It's quick & easy.

send file with resume download

P: n/a
I want to send a static file, via php to browser with ability to resume
download. This is the code:
--------------------------------------------
function send_file($file, $offset = 0, $offset2 = -1)
{
@$a = filesize($file);
if ($offset2 <= $offset || $offset2 < 1) $offset2 = $a - 1;
if ($offset2 <= $offset || $offset < 0) $offset = 0;

header("Content-Range: bytes $offset-$offset2/$a");
header('Content-Length: ' . ($offset2 - $offset + 1));

@$fp = fopen($file, 'rb') or die("could not open file <b>$file</b>. I
must die ;-(");
fseek($fp, $offset);
$a = 1 + $offset2 - $offset; $b = (int) floor($a / 8192); $a &= 8191;
//HERE!!!

for ($z = 0; $z < $b; $z++) echo @fread($fp, 8192); //HERE!!!
echo @fread($fp, $a);
fclose($fp);
}
---------------------------------------------
It seems to work but not.
With this code, when I click to download with a download manager like
Free Download Manager (with segmented download), it hangs and downloads
nothing.

If I change lines commented "HERE!!!" to:
---------------------------------------------
$a = 1 + $offset2 - $offset; // $b = (int) floor($a / 8192); $a &= 8191;

//for ($z = 0; $z < $b; $z++) echo @fread($fp, 8192);
---------------------------------------------
then all work fine!

The problem is that:
if file is very big (e.g. 100 MBs) then the line
echo @fread($fp, $a);
presses the server memory
Where is the problem?

Thanks
Sep 5 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Chameleon wrote:
I want to send a static file, via php to browser with ability to resume
download. This is the code:
Try http://pear.php.net/package/HTTP_Download instead. HTTP partial
retrieval is hard to get right.

Sep 5 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.