PHP: output very slowly

I want to imitate a very slow server.

How can I make PHP send output very slowly?  I want it to send a few bytes then sleep then send a few more bytes.
<?php 

$image = 'image.png';

header('HTTP/1.1 200 OK', true);
header('Content-Length: ' . filesize($image) , true);
header('Content-Type: image/png', true);
header('Content-Disposition: inline; filename="image1.png"', true);
header('X-Powered-By: XYZ Digital Asset Management', true);
header("Cache-Control: no-cache, must-revalidate", true);
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT", true);
header('Cache-Control: private', true);

readfile($image);

exit;

?>

Open in new window

LVL 16
hankknightAsked:
Who is Participating?
 
Rik-LeggerConnect With a Mentor Commented:
$fh = fopen($local_file, "r");
while(!feof($fh)) 
{
    // output file without bandwidth limiting
    print(fread($fh, 51200)); // 51200 bytes = 50 kB
    sleep(1);
}

Open in new window


Should have a speedlimit of 50kbps
0
 
bartvdConnect With a Mentor Commented:
replace readfile($image); with:

$fh = fopen($image, 'r');
$theData = fread($fh, filesize($$image));
fclose($fh);

for($i=0;$i<100;$i++)
{
echo substr($theData,(i*filesize($$image))/100,filesize($$image))/100);
time_nanosleep(0, 100000);
}

This will split the file in 100 pieces and wait 0.1 sec between them
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.