Skip to content

Instantly share code, notes, and snippets.

@vovadocent
Created July 6, 2017 12:03
Show Gist options
  • Save vovadocent/d7b6d7fc06fea96d9cff68472ae6c1d3 to your computer and use it in GitHub Desktop.
Save vovadocent/d7b6d7fc06fea96d9cff68472ae6c1d3 to your computer and use it in GitHub Desktop.
Fast file download from remote url, using CURL
<?php
//$imurl - віддалений url файла
//$picpath - path до локального файла з новим іменем
function save_image($imurl, $picpath) {
$ch = curl_init();
$fp = fopen($picpath, 'w');
$ch = curl_init($imurl);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_ENCODING, "");
$res = curl_exec($ch);
curl_close($ch);
fclose($fp);
if (!$res || filesize($picpath) <= 1024) {
unlink($picpath);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment