|
Posted by J.O. Aho on 10/11/93 11:54
Lupus Yonderboy wrote:
> "J.O. Aho" <user@example.net> ha scritto nel messaggio
> news:4j6hp7F6cjitU1@individual.net...
>> in your php.ini you limit the amount of memory that a php script can use,
>> this limits the size of things for you, say around 20Mb in your case.
>
> that's too bad, since the script resides in a hosted space and I don't have
> admin rights...
Then you are out of luck as long as you use readfile() and even fpassthru().
>> I think fpassthru() uses less memory, as long as you don't use
>> ob_start()/ob_flush()
>
> since I'm not sure what you mean by "less", considering I won't be able to
> change that memory limit, do you think this could solve my problem, or
> should I just give up and stick with traditional download with direct links?
You will need to read a small portion of the file, pass it for output and then
flush the output buffer and read yet more of the file, flush the output buffer
and loop this until you sent the whole file.
Here is a function made by "Chris"[1] that will work kind of readfile(), but
see to that it's sent in smaller chunks and output buffer is flushed, so that
you can send larger files than the limitation of memory usage in php.ini.
<?php
function readfile_chunked($filename,$retbytes=true) {
$chunksize = 1*(1024*1024); // how many bytes per chunk
$buffer = '';
$cnt =0;
// $handle = fopen($filename, 'rb');
$handle = fopen($filename, 'rb');
if ($handle === false) {
return false;
}
while (!feof($handle)) {
$buffer = fread($handle, $chunksize);
echo $buffer;
ob_flush();
flush();
if ($retbytes) {
$cnt += strlen($buffer);
}
}
$status = fclose($handle);
if ($retbytes && $status) {
return $cnt; // return num. bytes delivered like readfile() does.
}
return $status;
}
?>
Add the function to your script, change the @readfile($file); to
readfile_chunked($file); and you should notice that big download will work.
//Aho
[1]: You will find "Chris" original post at
http://www.php.net/manual/en/function.readfile.php#54295
Navigation:
[Reply to this message]
|