Hello!
I've written a small download script whose primary function is to monitor for available monthly bandwidth and decide whether to allow files or not.
The files are large, ie. from 60MB up to a few hundred.
The file sending procedure is actually a loop while (!feof(input file)), sending 64kb of data on each iteration. I chose 64kb because I think it's the optimal size between bandwidth precision and server straining, because if someone aborts during an iteration, the transfer will not get recorded in the database, and hence the 64kb margin of error PER DOWNLOAD.
My question here is about flushing and limiting script execution time. So far, my script handles no flushing nor time limit. I've tested it and I was able to download using a download manager (yes, script solves for partial downloads) for a 115MB file, however the manager did report connection lost on a couple of occasions. Not only that, but I could not access my server while the test file was being downloaded using 4 download manager "sessions" at once -- I received 501 Internal error....
Server PHP timeout default is 30seconds. Not running in safe mode.
So, since I never worked with flushing, I have no idea what exactly is it for, or what is the difference between flush() and ob_flush().
The loop:
while (!feof($fd)) {
$buffer= fread ($fd, 65536); // read in 64k chunks
print $buffer; // send to browser
$bytesSent= strlen($buffer);
$res= mysql_query ("update query here"); // update bandwidth just used
}
How is the loop working? Is print waiting for 64k to be sent before the script continues? Since the file download starts immediately, and I am sure PHP did not buffer ALL 115MB of download, I guess there was no buffering involved, or am I wrong?
Should I issue set_time_limit(20) at the beginning of each iteration?
Should I flush or ob_flush (?difference?) at the end of each iteration?
Could the absence of flushing and time limiting account for 501 errors that I got, not only when trying to access the same script (during the download) but when I tried accessing ANYTHING on my website -- all pages are in PHP.
Thank you in advance, any help is extremely appreciated!
_void()