Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

How do I download files of big sizes from somewhere on the web to the web server with PHP? Also, what should be allowed on the server in order to make this happen? Thanks.

share|improve this question

1 Answer 1

You need CURL module installed on PHP.

Then create a folder on the server to save the downloaded files and set write permissions so that the webserver can write to that folder. Then just modify this PHP-script to download whatever file you want, just change the URL and the target filename (my example downloads Opera 10 disk image for Mac):

<?php
$url = 'http://www.opera.com/download/get.pl?id=32242&location=238&nothanks=yes&sub=marine';
$filename = 'temp/opera.dmg';
$fp = fopen($filename,'w+');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
curl_close($ch);
fclose($fp);
?>

The CURLOPT_TIMEOUT sets the maximum time CURL can use to download the file, note also that if the downloads take long time to finish, you might need to modify the PHP execution timeout in php.ini.

share|improve this answer
    
50 mins or secs? –  ThisIsMyselfFromRussia Sep 18 '09 at 13:41
    
How do I know how many secs it will need??? –  ThisIsMyselfFromRussia Sep 18 '09 at 13:47
    
The timeout is in secs. And you just need to calculate the maximum time the download might take. Just estimate your download speed, divide the maximum filesize with that and add some for safety and that is the timeout value you need. –  Raynet Sep 18 '09 at 14:02
    
And what if I cut out the timeout option? Would it hurt? –  ThisIsMyselfFromRussia Sep 18 '09 at 14:34
    
Only 2M out 10M have been downloaded. 2M is set as upload_max_filesize in php.ini on the server. How to change? –  ThisIsMyselfFromRussia Sep 18 '09 at 14:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.