473,397 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 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 3537
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.