Well, I took a function to read a file in chunks I noticed on php.net and concluded that I could limit the download speed if I cause the script to sleep after a chunk is read. I'm testing this on Windows XP Professional (WAMP5). The problem is, while the image file is being read, I can't load another page, it'll hang until the file is finished reading.
Any suggestions are welcome, even if you can't fix this script.PHP Code:function readfile_chunked($filename,$chunksize,$sleep=false,$retbytes=true) {
$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($sleep){
sleep(1);
}
if ($retbytes) {
$cnt += strlen($buffer);
}
}
$status = fclose($handle);
if ($retbytes && $status) {
return $cnt; // return num. bytes delivered like readfile() does.
}
return $status;
}
$DOWNLOAD_SPEED = 10;
$chunksize = round($DOWNLOAD_SPEED*1024); //should limit the download to 10kib/s
readfile_chunked($data['location'],$chunksize,true);
![]()




Bookmarks