Advertisement
reenadak

copy from url

Feb 20th, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.26 KB | None | 0 0
  1.    
  2.     /*
  3.     - Copy From URL
  4.     - Clones a file from the URL provided and places the copy in your server.
  5.    
  6.     - ! Requires cURL ! -
  7.    
  8.     $url         - The full URL path to the file to copy
  9.     $newLocation - The directory location relative to the location of this script to
  10.                    place the cloned file.
  11.     $newName     - Optional, the name (and the extension!) the cloned file should be
  12.                    renamed to. If left blank the name will stay the same.
  13.     */
  14.    
  15.     public function copyFromUrl($url, $newLocation, $newName = '', $timeOutSecs = 300) {
  16.         $curl = curl_init($url);
  17.        
  18.         if ($newName == '') {
  19.             $oldName = explode("/", $url);
  20.             $newName = $oldName[count($oldName) - 1];
  21.         }
  22.        
  23.         if (substr($newLocation, -1) != '/')
  24.             $newLocation .= '/';
  25.        
  26.         $file = fopen($newLocation . $newName, 'wb');
  27.         curl_setopt($curl, CURLOPT_FILE, $file);
  28.         curl_setopt($curl, CURLOPT_HEADER, 0);
  29.         curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
  30.         curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 0);
  31.         curl_setopt($curl, CURLOPT_TIMEOUT, $timeOutSecs);
  32.         $resp = curl_exec($curl);
  33.        
  34.         if (curl_errno($curl))
  35.             echo "cURL Error: " . curl_error($curl);
  36.         elseif ($resp !== false) {
  37.             curl_close($curl);
  38.             fclose($file);
  39.             return true;
  40.         }
  41.        
  42.         return false;
  43.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement