I’ve got a download script which checks a couple of things and then streams a file across in chunks of 8kb.
The loop that does the transfer looks like:
$file = @fopen($file_path,"rb");
if ($file) {
while(!feof($file)) {
set_time_limit(60);
print(fread($file, 1024*8));
flush();
if (connection_status()!=0) {
@fclose($file);
die();
}
}
@fclose($file);
}
I wrote a small application which simulated a very slow download. It waits for 2 minutes before continuing the download. I expected that the script would time out given that I’ve set a 60 second time limit. This does not happen and the download continues until it has finished. It seems that the time spent in print / flush doesn’t count towards the script execution time. Is this correct? Is there a better way to send the file to the client / browser such that I can specify a time limit for the print / flush command?
From
set_time_limit():So it looks like you can either measure the passage of real time with calls to the
time()function, along the lines of:Or you can use Windows. I prefer the first option :p