472,952 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,952 software developers and data experts.

Download with a timeout

Hello,

I am trying to download a few files one after another from a remote
server. Problem is that I don't know how to, or if I am able at all,
set a time out for download. I don't want to time out whole script,
just a part if file won't download in 2mins then skip to the next one.
Previously, I had a Javascript implementation with AJAX, this time I
thought of doing it in PHP since PHP has far better array functions, I
don't have to do most from scratch. Yet, I with PHP implementation I
came to a problem where I am unable to cancel the download if it runs
more than so many minutes. Yet, as I said, I don't want to quit the
script completely, all I need is to skip to the next file and try that.

Any help? Please?

Sep 20 '06 #1
1 3477
Hi,

One way to download files in PHP is to use the CURL extension
(php.net/curl). This allows you to transfer files with a timeout. The
options are pretty straightforward. Below is a function to download
files over HTTP with CURL. This can be called like so:

downloadUrl('http://www.yahoo.com', 'yahoo.html', 10);

The URL http://www.yahoo.com will be downloaded to the file yahoo.html.
The time limit is 10 seconds.

Source code is below:

function curlDownload($url, $destination, $timeout)
{
// The result array will store the download status
// and various statistics

$result = array(
'completed' =true,
'timedOut' =false,
'errors' =array(),
'curlErrorNum' =0,
'curlStats' =null
);

// Open the local destination file

if (!($fp = fopen($destination, 'w'))) {
$result['completed'] = false;
$result['errors'][] = "Failed to open file \"$destination\" for
writing.";
return $result;
}

// Initialize CURL with the URL

$ch = curl_init($url);

// Set the destination file handle

curl_setopt($ch, CURLOPT_FILE, $fp);

// Don't write the HTTP header to the output file

curl_setopt($ch, CURLOPT_HEADER, 0);

// Set a timeout

curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);

// Execute the request

curl_exec($ch);

// Close the destination file

if (!fclose($fp)) {
unlink($destination);
$result['completed'] = false;
$result['errors'][] = "Failed to close file \"$destination\".";
}

// Get the CURL error status

$result['curlErrorNum'] = curl_errno($ch);

if ($result['curlErrorNum'] != 0) {
unlink($destination);
$result['completed'] = false;
$result['errors'][] = curl_error($ch);

// Check for a request time out

if ($result['curlErrorNum'] == 28) {
$result['timedOut'] = true;
}
}

// Get download statistics

$result['curlStats'] = curl_getinfo($ch);

// Make sure the HTTP status code from the server is valid

if ($result['curlStats']['http_code'] != 200) {
unlink($destination);
$result['completed'] = false;
$result['errors'][] = "Received HTTP code
{$result['curlStats']['http_code']}.";
}

curl_close($ch);

return $result;
}

function downloadUrl($url, $destination, $timeout)
{
$result = curlDownload($url, $destination, $timeout);

if ($result['timedOut']) {
// Request timed out
$errors = join($result['errors'], " ");
echo "Download of URL {$result['curlStats']['url']} timed out:
$errors";
} elseif (!$result['completed']) {
// Another error occurred
$errors = join($result['errors'], " ");
echo "Download of URL {$result['curlStats']['url']} failed:
$errors";
} else {
// Download was successful
echo "Downloaded " .
number_format($result['curlStats']['size_download']) .
" bytes from URL " . $result['curlStats']['url'] . " in " .
number_format(round($result['curlStats']['total_time'], 2), 2) .
" seconds at a rate of " .
number_format($result['curlStats']['speed_download']) .
" bytes per second.";
}

print_r($result);

return $result;
}

downloadUrl('http://www.yahoo.com', 'yahoo.com.html', 10);

Best Regards,

John Peters

a.********@gmail.com wrote:
Hello,

I am trying to download a few files one after another from a remote
server. Problem is that I don't know how to, or if I am able at all,
set a time out for download. I don't want to time out whole script,
just a part if file won't download in 2mins then skip to the next one.
Previously, I had a Javascript implementation with AJAX, this time I
thought of doing it in PHP since PHP has far better array functions, I
don't have to do most from scratch. Yet, I with PHP implementation I
came to a problem where I am unable to cancel the download if it runs
more than so many minutes. Yet, as I said, I don't want to quit the
script completely, all I need is to skip to the next file and try that.

Any help? Please?
Sep 20 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Satinderpal Singh | last post by:
Hi All, To download the files from the http or https server, we have used WebClient object. But, the problem is we cannot specify timeout in the WebClient object. So, using HttpWebRequest...
9
by: Hasani \(remove nospam from address\) | last post by:
I have a website with 2 aspx pages Foo.aspx, and bar.aspx The content of both files is //in 1 file Hello <%=Session.ToString()%> ================
0
by: bonita | last post by:
If I add the code for user to download the file (e.g. if(File.Exists(FILE_NAME)){......}), the ASP.NET will give the following timeout error: Timeout expired. The timeout period elapsed prior to...
0
by: Tim Cowan | last post by:
Hi, I am using this code to download files on VS 2005. It downloads the file but waits until the timeout (which is why it is so short) to add the last 4 bytes or so. It always returns a...
2
by: cburkhar | last post by:
Hi all, I'm having a terrible time downloading a file from IIS5 when the site is using .net 2.x. Web page: <html> <body> <a href="log.log">dl log</a> </body> </html> Where log.log is a 60MB...
3
by: Piotrekk | last post by:
Hi My question is: What is the best way to download file giving it maximum timeout ( for example 30 minutes ). After this time the operation should be terminated ( maybe exception ? ). I was...
10
by: =?Utf-8?B?SnVhbg==?= | last post by:
Hi! I want to use ASP to download big files using ADODB.STREAM. It works very fine with files smaller than 80 MB. On the Webserver I can see that memory allocation and the process w3wp is...
1
by: Tim Jones | last post by:
I have a web site where we offer MP3 downloads (yes, they are legal!). I've written a PHP script using readfile() (or fpassthru()) that sends the file using HTTP headers (via various header()...
11
by: raamay | last post by:
when i download movies my download stops in the middle. I use Free Download Manager and i get the message "File not found in the server" error. The download manger tries to reconnect to server...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.