2011-06-25 40 views
32

URLからCURLを使用して画像を保存し、それを自分のサーバー上のフォルダに保存する必要があります。私はこのコードを無駄に戦ってきました。理想的には、画像をつかんで「写真1」などの名前で保存したいと思います。助けて!URLからcurlで画像を保存する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

うまくコーディングされたWebサイトがユーザーエージェントを探すことに注意してください。すべてのブラウザ、タブレット、または携帯電話には常にユーザーエージェントが用意されています。それでもなお画像を取得できない場合、おそらくユーザエージェントの検出のために、これを追加してください... '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オプションを使用して、ファイルをカールに直接表示してダウンロードすることができます。ここで

は関数である。

// 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; 
} 

そして、ここでは、この関数を呼び出す方法です:Komangの答えの

// 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

改良版(ファイルを書き込むことができますかどうかを確認、リファラやユーザーエージェントを追加)、それは大丈夫だ場合は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); 
    } 
} 
関連する問題