Skip to content

Instantly share code, notes, and snippets.

@liconti
Created April 20, 2012 06:33
Show Gist options
  • Save liconti/2426607 to your computer and use it in GitHub Desktop.
Save liconti/2426607 to your computer and use it in GitHub Desktop.
PHP dowload file (following redirects)
<?php
function download_file($url, $filename){
$ch = curl_init($url);
if (is_writable(dirname($filename))) {
$fp = fopen($filename, "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
curl_close($ch);
fclose($fp);
} else {
echo 'The file ($filename) is not writable';
die();
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment