|
Posted by Chameleon on 12/16/48 11:57
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
Navigation:
[Reply to this message]
|