Copy a remote file to local server

  $FileUrl = "http://mydomain.com/myimage.jpg";
  $ch = curl_init($FileUrl);
  $FileName = basename($FileUrl);    // Get the file name and extension using basename()
  $SavePath = $_SERVER['DOCUMENT_ROOT'] . '/myfolder/mysavedfilename.jpeg';
  $fp = fopen($SavePath, 'wb');
  
  curl_setopt($ch, CURLOPT_FILE, $fp);    // Set the file transfer option and header to CURL
  curl_setopt($ch, CURLOPT_HEADER, 0);

  $CurlResult = curl_exec($ch);     // Execute the CURL session
  fclose($fp); 

  //Check for curl errors
  if ($CurlResult === False)
  {
    fclose($fp);
    return(False);
  }

  //Check HTTP response code
  $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  fclose($fp);

  if ($http_code !== 200)
  {
    unlink($SavePath);        //Delete possibly incomplete file
    return(False);
  }