Advertisement
Guest User

Untitled

a guest
Aug 7th, 2012
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.15 KB | None | 0 0
  1.   function download_remote_file($src, $dest)
  2.   {
  3.     if (empty($src))
  4.     {
  5.       return false;
  6.     }
  7.    
  8.     $return = ($dest === true) ? true : false;
  9.    
  10.     /* curl */
  11.     if (!function_exists('curl_init'))
  12.     {
  13.       if (!$return)
  14.       {
  15.         $newf = fopen($dest, "wb");
  16.       }
  17.       $ch = curl_init();
  18.      
  19.       curl_setopt($ch, CURLOPT_URL, $src);
  20.       curl_setopt($ch, CURLOPT_HEADER, 0);
  21.       curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept-language: en"));
  22.       curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)');
  23.       curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  24.       curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
  25.       curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  26.       if (strpos($src, 'https://') !== false)
  27.       {
  28.         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  29.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  30.       }
  31.       if (!$return)
  32.       {
  33.         curl_setopt($ch, CURLOPT_FILE, $newf);
  34.       }
  35.       else
  36.       {
  37.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  38.       }
  39.      
  40.       $out = curl_exec($ch);
  41.       curl_close($ch);
  42.      
  43.       if ($out === false)
  44.       {
  45.         return 'file_error';
  46.       }
  47.       else if (!$return)
  48.       {
  49.         fclose($newf);
  50.         return true;
  51.       }
  52.       else
  53.       {
  54.         return $out;
  55.       }
  56.     }
  57.     /* file get content */
  58.     else if (ini_get('allow_url_fopen'))
  59.     {
  60.       if (strpos($src, 'https://') !== false and !extension_loaded('openssl'))
  61.       {
  62.         return false;
  63.       }
  64.      
  65.       $opts = array(
  66.         'http' => array(
  67.           'method' => "GET",
  68.           'user_agent' => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)',
  69.           'header' => "Accept-language: en",
  70.         )
  71.       );
  72.  
  73.       $context = stream_context_create($opts);
  74.      
  75.       if (($file = file_get_contents($src, false, $context)) === false)
  76.       {
  77.         return 'file_error';
  78.       }
  79.      
  80.       if (!$return)
  81.       {
  82.         file_put_contents($dest, $file);
  83.         return true;
  84.       }
  85.       else
  86.       {
  87.         return $file;
  88.       }
  89.     }
  90.    
  91.     return false;
  92.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement