- Details
-
Category: Blog
-
Created on 27 March 2012
-
Written by Roberto Carreon
Download a file and save it locally with cURL
3.4 out of
5
based on
48 votes.
A fragment of code that will help you to download a file from anywhere in the web and to save it locally in your webserver.
# Set your own path and filename
$localFile = '/home/any/path/filename.txt';
# Set the URI for the file to download
$remoteFile = 'http://www.elwebserver.com/path/to/file.txt';
if ($fp = @fopen ($localFile, 'w+'))
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $remoteFile);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
if(curl_errno($ch))
{
echo 'ERROR: ' . curl_error($ch);
}
curl_close($ch);
fclose($fp);
}