I’ve created a script that downloads very large files from the internet.
Something quite simple:
<?php
$source = fsockopen(…); // http
$destination = fopen(…,'wb'); // file
while (!feof($source))
{
fwrite($destination,fread($source,4096));
}
fclose($source);
fclose($destination);
?>
It works great but this script uses 100% CPU on my server when downloading about 10 MB per second. Is it normal?
It is an Intel Xeon Quad Core X3323 @ 2.50 GHz.
P.S. It is in fact a little bit more complicated because my script reads HTTP headers first, but it doesn’t matter.
In most cases, you shouldn’t implement HTTP with sockets in the first place. Instead, use curl or simply fopen wrappers, like
However, if you insist to do your own socket handling, note that
freadwill return an empty string if there is nothing to read. If your socket is in nonblocking mode, fread will immediately return''. You can callsocket_set_blockto put the socket back in blocking mode: