PHP下載/採集遠程圖片到本地

來源:互聯網
上載者:User

標籤:

/** * 下載遠程圖片到本地 * * @param string $url 遠程檔案地址 * @param string $filename 儲存後的檔案名稱(為空白時則為隨機產生的檔案名稱,否則為原檔案名稱) * @param array $fileType 允許的檔案類型 * @param string $dirName 檔案儲存的路徑(路徑其餘部分根據時間系統自動產生) * @param int $type 遠程擷取檔案的方式 * @return json 返迴文件名、檔案的儲存路徑 * @author blog.snsgou.com */function download_image($url, $fileName = ‘‘, $dirName, $fileType = array(‘jpg‘, ‘gif‘, ‘png‘), $type = 1){if ($url == ‘‘){return false;}// 擷取檔案原檔案名稱$defaultFileName = basename($url);// 擷取檔案類型$suffix = substr(strrchr($url, ‘.‘), 1);if (!in_array($suffix, $fileType)){return false;}// 設定儲存後的檔案名稱$fileName = $fileName == ‘‘ ? time() . rand(0, 9) . ‘.‘ . $suffix : $defaultFileName;// 擷取遠程檔案資源if ($type){$ch = curl_init();$timeout = 30;curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);$file = curl_exec($ch);curl_close($ch);}else{ob_start();readfile($url);$file = ob_get_contents();ob_end_clean();}// 設定檔案儲存路徑//$dirName = $dirName . ‘/‘ . date(‘Y‘, time()) . ‘/‘ . date(‘m‘, time()) . ‘/‘ . date(‘d‘, time());$dirName = $dirName . ‘/‘ . date(‘Ym‘, time());if (!file_exists($dirName)){mkdir($dirName, 0777, true);}// 儲存檔案$res = fopen($dirName . ‘/‘ . $fileName, ‘a‘);fwrite($res, $file);fclose($res);return array(‘fileName‘ => $fileName,‘saveDir‘ => $dirName);}

 

 

實戰經曆:

部落格中有些圖片是直接引用其他網站的,這些天不知道咋地,估計是對方做了防盜鏈操作,導致部落格中的圖片顯示不出來,沒辦法,只好用PHP批量採集下來,並且批量替換博文中的圖片地址:

/** * 批量下載部落格中的圖片到本地 */public function index(){global $_G;$blogModel = model(‘Blog‘, ‘blog‘);$list = $blogModel->order(‘gid desc‘)->limit(10)->findPage();$page = get_gpc(‘page‘) ? get_gpc(‘page‘) : 1;$totalPages = $list[‘totalPages‘];$page = $page + 1;if ($page > $totalPages){die(‘更新完畢!‘);}foreach ($list[‘data‘] as $val){$content = $val[‘content‘];$excerpt = $val[‘excerpt‘];$_G[‘isContentUpdate‘] = $_G[‘isExcerptUpdate‘] = false;/* 內容 */$content = preg_replace_callback("/src=\"(http:\/\/images\.cnblogs\.com\/cnblogs_com[^\"]+)\"/", function($matches) {global $_G;$_G[‘isContentUpdate‘] = true;// 下載遠程圖片到本地$res = download_image($matches[1], ‘old‘, ‘d:/PHP/xampp/htdocs/emlog/data/upload‘);// 返回 下載後的圖片url地址return ‘src="/data/upload/‘ . date(‘Ym‘, time()) . ‘/‘ . $res[‘fileName‘] . ‘"‘;}, $content);/* 摘要 */$excerpt = preg_replace_callback("/src=\"(http:\/\/images\.cnblogs\.com\/cnblogs_com[^\"]+)\"/", function($matches) {global $_G;$_G[‘isExcerptUpdate‘] = true;// 下載遠程圖片d到本地$res = download_image($matches[1], ‘old‘, ‘d:/PHP/xampp/htdocs/emlog/data/upload‘);// 返回 下載後的圖片url地址return ‘src="/data/upload/‘ . date(‘Ym‘, time()) . ‘/‘ . $res[‘fileName‘] . ‘"‘;}, $excerpt);/* 更新資料庫 */$where = array(‘gid‘ => $val[‘gid‘]);$data = array();if ($_G[‘isContentUpdate‘]){$data[‘content‘] = $content;}if ($_G[‘isExcerptUpdate‘]){$data[‘excerpt‘] = $excerpt;}if ($data){$blogModel->where($where)->save($data);}}/* 更新下一頁 */$url = url(‘blog/Main/index‘, array(‘page‘ => $page));$msg = ‘正在更新‘ . $page . ‘/‘ . $totalPages;redirect($url, 2, $msg);}

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.