In many situations you want to download a file (or a page) from another server to your server. You can do this with cURL! Here is a function which does that, written in PHP.
<?php
function downloadFile($url, $path='', $file='', $options = array())
{
//make sure the options is an array
if (!is_array($options)) $options = array();
//add default options. You can remove these from here, but make sure you give the options you removed, when you call this method
$options = array_merge(array(
'connectionTimeout' => 5, // seconds
'timeout' => 10, // seconds
'sslVerifyPeer' => false,
'followLocation' => false, // if true, limit recursive redirection by
'maxRedirs' => 1, // setting value for "maxRedirs"
), $options
);
// if no filename given, create a random filename (with no extension)
if (empty($filename)) $filename = rand();
// create a file (we are assuming that we can write to the system's directory)
$FileName = $path.$filename;
$fh = fopen($FileName, 'w');
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_FILE, $fh);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $options['connectionTimeout']);
curl_setopt($curl, CURLOPT_TIMEOUT, $options['timeout']);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $options['sslVerifyPeer']);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, $options['followLocation']);
curl_setopt($curl, CURLOPT_MAXREDIRS, $options['maxRedirs']);
curl_exec($curl);
curl_close($curl);
fclose($fh);
return $FileName;
}
?>





