My script was based on example 3b, but used up 100% CPU when a timeout occurred that wasn't "seen". So here's my code, hoping this will help people out there with the same problem.Obviously first use $r Page = fsockopen(...) and fwrite($r Page,...) and such, after which: $s Page = ''; // the page goes in here $i Timeout = 5; // set the timeout in seconds stream_set_timeout($r Page,$i Timeout); stream_set_blocking($r Page,0); $f Timeout = microtime(true); do while (! $a Info['timed_out'] and microtime(true)-$f Timeout Note to IIS admins: When using PHP via the Fast CGI ISAPI extension, there is a script timeout of approximately 1hr that cannot be adjusted.If you serve a file download over PHP with fread and print/echo and experience corrupted binary files, chances are the server still uses magic quotes and escapes the null bytes in your file.
Try to turn them off by placing this code before using fread: When you read a file whose size is a multiple of the readsize (8192 here), then the loop is executed when there are no more data to read.Here, the result of fread() is not checked, and so the instruction Changing the value of $length may yield to different download speeds when serving a file from a script.When using PHP via CGI, there is a script timeout that is based upon the value of the CGITimeout configuration option.This value must be set extremely high if you plan to serve large files.Irritating for me because I was using simple_xml_load to load the file contents as XML, and the problem initially appeared to be that function.
Solution - swap over to file_get_contents or use the loop suggested on the documentation above (see Warning).I was not able to max out my 10mbps connection when 4096 was used.I found out that using 16384 would use all the available bandwidth.- Sends a number of headers, including ones that ensure it's cached for a maximum of 2 hours on any browser/proxy, and "Content-Length" which most people seem to forget.(tested on Linux (Apache) and Windows (IIS5/6) under PHP4.3.x)Note that the folder from which protected files will be pulled, is set as a constant in this function (/protected) ...Documentation: Apache module: https://tn123.org/mod_xsendfile/Nginx: Lighttpd: this will save you many hours of work.