2011-06-25 58 views
32

我需要使用CURL從URL保存圖像並將其保存到我的服務器上的文件夾。我一直在與這個代碼作鬥爭,無濟於事。理想情況下,我想抓取圖像並將其保存爲「photo1」或其他內容。幫幫我!從捲曲URL保存圖像PHP

function GetImageFromUrl($link) 

    { 

    $ch = curl_init(); 

    curl_setopt($ch, CURLOPT_POST, 0); 

    curl_setopt($ch,CURLOPT_URL,$link); 

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

    $result=curl_exec($ch); 

    curl_close($ch); 

    return $result; 

    } 

    $sourcecode = GetImageFromUrl($iticon); 

    $savefile = fopen(' /img/uploads/' . $iconfilename, 'w'); 
    fwrite($savefile, $sourcecode); 
    fclose($savefile); 

回答

78

試試這個:

function grab_image($url,$saveto){ 
    $ch = curl_init ($url); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1); 
    $raw=curl_exec($ch); 
    curl_close ($ch); 
    if(file_exists($saveto)){ 
     unlink($saveto); 
    } 
    $fp = fopen($saveto,'x'); 
    fwrite($fp, $raw); 
    fclose($fp); 
} 

,並確保在php.ini中allow_url_fopen選項是讓

+0

謝謝!我會繼續嘗試這件事情,看看它是否有效。 – David

+11

請記住編碼良好的網站將查找用戶代理。每個瀏覽器,平板電腦或手機都會有一個用戶代理!如果您仍然無法獲取圖像,最有可能是因爲用戶代理檢測,請添加...'curl_setopt($ ch,CURLOPT_USERAGENT,'MyImage Collector + http://www.yourdomainname/mybot.html'); '或欺騙一個真正的'curl_setopt($ ch,CURLOPT_USERAGENT,'Mozilla/5.0(Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13)Gecko/20080311 Firefox/2.0.0.13');' – bbullis

+0

像這樣保存圖像時是否有任何文件大小限制? PHP配置中的文件上傳限制是否會影響到這一點? – Foreever

21

選項#1

而是選擇二進制/原始數據組合成的變量然後寫入,你可以使用CURLOPT_FILE選項直接顯示一個文件到curl下載。

下面是函數:

// takes URL of image and Path for the image as parameter 
function download_image1($image_url, $image_file){ 
    $fp = fopen ($image_file, 'w+');    // open file handle 

    $ch = curl_init($image_url); 
    // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // enable if you want 
    curl_setopt($ch, CURLOPT_FILE, $fp);   // output to file 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 1000);  // some large value to allow curl to run for a long time 
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0'); 
    // curl_setopt($ch, CURLOPT_VERBOSE, true); // Enable this line to see debug prints 
    curl_exec($ch); 

    curl_close($ch);        // closing curl handle 
    fclose($fp);         // closing file handle 
} 

這裏是你應該怎麼稱呼它:

// test the download function 
download_image1("http://www.gravatar.com/avatar/10773ae6687b55736e171c038b4228d2", "local_image1.jpg"); 

選項#2

現在,如果你想下載非常大的文件,以上情況功能可能不會變得方便。您可以使用下面的函數來處理大文件。另外,如果需要,您可以打印進度(以%或任何其他格式)。下面的功能是通過使用callback函數來實現的,該函數將大塊數據寫入文件中,以進行下載。

// takes URL of image and Path for the image as parameter 
function download_image2($image_url){ 
    $ch = curl_init($image_url); 
    // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // enable if you want 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 1000);  // some large value to allow curl to run for a long time 
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0'); 
    curl_setopt($ch, CURLOPT_WRITEFUNCTION, "curl_callback"); 
    // curl_setopt($ch, CURLOPT_VERBOSE, true); // Enable this line to see debug prints 
    curl_exec($ch); 

    curl_close($ch);        // closing curl handle 
} 

/** callback function for curl */ 
function curl_callback($ch, $bytes){ 
    global $fp; 
    $len = fwrite($fp, $bytes); 
    // if you want, you can use any progress printing here 
    return $len; 
} 

這裏是如何調用該函數:

// test the download function 
$image_file = "local_image2.jpg"; 
$fp = fopen ($image_file, 'w+');    // open file handle 
download_image2("http://www.gravatar.com/avatar/10773ae6687b55736e171c038b4228d2"); 
fclose($fp);         // closing file handle 
1

改進Komang答案(添加引用者和用戶代理,請檢查您是否可以寫入文件),返回true,如果它是確定的版本,如果出現錯誤,則返回false:

public function downloadImage($url,$filename){ 
    if(file_exists($filename)){ 
     @unlink($filename); 
    } 
    $fp = fopen($filename,'w'); 
    if($fp){ 
     $ch = curl_init ($url); 
     curl_setopt($ch, CURLOPT_HEADER, 0); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
     curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); 
     $result = parse_url($url); 
     curl_setopt($ch, CURLOPT_REFERER, $result['scheme'].'://'.$result['host']); 
     curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0'); 
     $raw=curl_exec($ch); 
     curl_close ($ch); 
     if($raw){ 
      fwrite($fp, $raw); 
     } 
     fclose($fp); 
     if(!$raw){ 
      @unlink($filename); 
      return false; 
     } 
     return true; 
    } 
    return false; 
} 
+1

'CURLOPT_RETURNTRANSFER'首先將整個文件讀取到一個php變量中。速度較慢,可能會破壞大文件。改爲使用'CURLOPT_FILE'傳遞文件指針。 – Walf

1

這是最簡單的實現。

function downloadFile($url, $path) 
{ 
    $newfname = $path; 
    $file = fopen($url, 'rb'); 
    if ($file) { 
     $newf = fopen($newfname, 'wb'); 
     if ($newf) { 
      while (!feof($file)) { 
       fwrite($newf, fread($file, 1024 * 8), 1024 * 8); 
      } 
     } 
    } 
    if ($file) { 
     fclose($file); 
    } 
    if ($newf) { 
     fclose($newf); 
    } 
}