php遠程下載圖片

來源:互聯網
上載者:User

標籤:www   圖片路徑   www.   match   迴圈   成功   zixue   尾碼   false   

這一天上班真的累啊,趁著這會閑寫個抓取圖片的碼子。話不多少上代碼!!


 1 <?php 2 //要抓取的網址; 3 $url = ‘http://www.zixue.it/‘;  4 //取回網頁內容; 5 $tmp = file_get_contents($url); 6 //正則擷取圖片地址; 7 preg_match_all(‘/<img[^>]*src="([^"]*(?:jpg|png|gif|jpeg))"[^>]*>/i‘,$tmp,$res); 8 //var_dump($res); 9 10 //去重11 $imgs = array_unique($res[1]);12 13 //迴圈儲存圖片14 foreach($imgs as $img){15 //如果圖片路徑為相對路徑則轉為絕對路徑;16     if(stripos($img,‘http://‘)===false){17      $img = $url.$img;18    }19     //圖片尾碼;20    $ext = pathinfo($img,PATHINFO_EXTENSION);21    if(!($tmp=file_get_contents($img))){22      continue;23    }24    if(file_put_contents(uniqid().‘.‘.$ext,$tmp)){25      echo "圖片".$img."下載成功!<br />";26    }else{27      echo "<font color=‘red‘>圖片".$img."下載失敗!</font><br />";28    }29 }30 ?>

原文地址:http://lixiangfeng.com/blog/article/content/7810007轉載請保留原文串連!!謝謝

php遠程下載圖片

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.