Advertisement
Guest User

Curl file download - PHP

a guest
Feb 25th, 2015
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.01 KB | None | 0 0
  1. function curlToFile($url, $filename, $download_folder) {
  2.                 if (!(file_exists($download_folder)))
  3.                     mkdir($download_folder);
  4.                 chdir($download_folder);
  5.                 $fp = fopen($filename, "w+");
  6.                 $curl = curl_init();
  7.                 curl_setopt($curl, CURLOPT_URL, $url);
  8.                 curl_setopt($curl, CURLOPT_POST, 1);
  9.                 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  10.                 curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
  11.                 curl_setopt($curl, CURLOPT_REFERER, "http://www.somedomain.com");
  12.                 curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.10) Gecko/20100504 Firefox/3.5.10");
  13.                 curl_setopt($curl, CURLOPT_FILE, $fp);
  14.                 $ret = curl_exec($curl);
  15.                 if (!$ret) {
  16.                     $this->lastError = curl_error($curl);
  17.                 }
  18.                 curl_close($curl);
  19.                 return $ret;
  20.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement