473,848 Members | 1,538 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3591
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('ht tp://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($u rl, $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($destinat ion, '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($destina tion);
$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($destina tion);
$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($c h);

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

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

curl_close($ch) ;

return $result;
}

function downloadUrl($ur l, $destination, $timeout)
{
$result = curlDownload($u rl, $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(r ound($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('ht tp://www.yahoo.com', 'yahoo.com.html ', 10);

Best Regards,

John Peters

a.********@gmai l.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
1294
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 and HttpWebResponse the only way to do it ? or is there some other way with which we can specify timeout for WebClient object.
9
2779
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
2040
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 completion of the operation or the server is not responding. It seems that the download file code is executing before letting the previous code to finish. Even the message print code (message.Text="...";) is not executed.
0
1641
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 WebException which most of the time is just feedback from the FTP server, so how do I tell if the download was successful/complete or not? Tim
2
1944
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 log file.
3
6237
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 thinking about simplest possible solution WebClient class but don't know if it is possible to implement timeout...
10
19388
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 running. After some time (more or less 2 minutes) I get a response timeout. Here is the code:
1
1240
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() directives). The script launches the download fine and the download proceeds normally. However, after 20MB - 35MB of the download completes, the download suddenly completes even though the entire file wasn't download. (The download does not fail.)...
11
17832
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 several times and then stops. It usually happens when i download large size videos of about 100 MB. The download stops at random point, sometimes when it is 45% complete and sometime when it is 98% complete. I have adsl broadband connection with download...
0
10996
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10658
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10347
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7062
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5730
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5915
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4541
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4133
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3171
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.