Downloading a large file using curl
<?phpset_time_limit(0);//This is the file where we save the information$fp = fopen (dirname(__FILE__) . '/localfile.tmp', 'w+');//Here is the file we are downloading, replace spaces with %20$ch = curl_init(str_replace(" ","%20",$url));curl_setopt($ch, CURLOPT_TIMEOUT, 50);// write curl response to filecurl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);// get curl responsecurl_exec($ch); curl_close($ch);fclose($fp);?>
I use this handy function:
By downloading it with a 4094 byte step it will not full your memory
function download($file_source, $file_target) { $rh = fopen($file_source, 'rb'); $wh = fopen($file_target, 'w+b'); if (!$rh || !$wh) { return false; } while (!feof($rh)) { if (fwrite($wh, fread($rh, 4096)) === FALSE) { return false; } echo ' '; flush(); } fclose($rh); fclose($wh); return true;}
Usage:
$result = download('http://url','path/local/file');
You can then check if everything is ok with:
if (!$result) throw new Exception('Download error...');
Find below code if you want to download the contents of the specified URL also want to saves it to a file.
<?php$ch = curl_init();/*** Set the URL of the page or file to download.*/curl_setopt($ch, CURLOPT_URL,'http://news.google.com/news?hl=en&topic=t&output=rss');$fp = fopen('rss.xml', 'w+');/*** Ask cURL to write the contents to a file*/curl_setopt($ch, CURLOPT_FILE, $fp);curl_exec ($ch);curl_close ($ch);fclose($fp);?>
If you want to downloads file from the FTP server you can use php FTP extension. Please find below code:
<?php$SERVER_ADDRESS="";$SERVER_USERNAME="";$SERVER_PASSWORD="";$conn_id = ftp_connect($SERVER_ADDRESS);// login with username and password$login_result = ftp_login($conn_id, $SERVER_USERNAME, $SERVER_PASSWORD);$server_file="test.pdf" //FTP server file path $local_file = "new.pdf"; //Local server file path ##----- DOWNLOAD $SERVER_FILE AND SAVE TO $LOCAL_FILE--------##if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) { echo "Successfully written to $local_file\n";} else { echo "There was a problem\n";}ftp_close($conn_id);?>