Saturday, January 17, 2009

File Fetcher in PHP

My net connection is too slow and inconsistent to download large files from a non-resume capable server. So I thought of creating a PHP file fetcher program, that I could upload on a fast PHP capable web server and it would download the file itself within a minute and then I could download from there in many sessions by resuming broken downloads. Here's what I coded:

<?php
$blah=fopen("dload", "a+");
$fp = fsockopen("host.com", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)
\n";
} else {
$out = "GET /filepath.ext HTTP/1.1\r\n";
$out .= "Host: host.com\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
$temp=fgets($fp, 128);
fwrite($blah, $temp);
}
fclose($fp);
fclose($blah);
}
?>


In the above code host.com is the hostname or IP of the server I'm trying to fetch the file from, and filepath.ext is the path and filename with extension of the file to be downloaded from there.
It works fine for files upto around 50MB on my server but for larger size it doesn't work. After thinking for a while I figured out that it may be due to the maximum execution time setting of 30 seconds in php.ini file. The server I used has practical speeds around 1.5MB/s and so it downloads a 45-50MB file in 30 seconds and then stops.
The only obvious way is to edit the php.ini file and increase the maximum execution time to something like 300 seconds. But I don't have access to php.ini on my server. What should I do now???? Well, if you've any solution then do post a comment here please.

No comments: