php采集內(nèi)容中帶有圖片地址的遠(yuǎn)程圖片并保存的方法
本文實(shí)例講述了php采集內(nèi)容中帶有圖片地址的遠(yuǎn)程圖片并保存的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
if ( function_exists('curl_init') )
{
$ch = curl_init();
curl_setopt ($ch, curlopt_url, $url);
curl_setopt ($ch, curlopt_returntransfer, 1);
curl_setopt ($ch, curlopt_connecttimeout, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
}
else if ( ini_get('allow_url_fopen') == 1 || strtolower(ini_get('allow_url_fopen')) == 'on' )
{
$file_contents = @file_get_contents($url);
}
else
{
$file_contents = '';
}
return $file_contents;
}
$img_array = array();
$img_path = realpath("../../../upfile/news/").'/'.date("y/m/d/"); //采集遠(yuǎn)程圖片保存地址
//die($img_path);
$img_rpath='/upfile/news/'.date("y/m/d/"); //設(shè)置訪問(wèn)地址
$body = stripslashes(strtolower($body));
preg_match_all("/(src|src)=["|'| ]{0,}(http://(.*).(gif|jpg|jpeg|png))/isu",$body,$img_array);
$img_array = array_unique($img_array[2]);
foreach ($img_array as $key => $value) {
$get_file = my_file_get_contents($value,60);
$filetime = time();
$filename = date("ymdhis",$filetime).rand(1,999).'.'.substr($value,-3,3);
if(emptyempty($get_file)){
@sleep(10);
$get_file = my_file_get_contents($value,30);
if(emptyempty($get_file)){
$body = preg_replace("/".addcslashes($value,"/")."/isu", '/notfound.jpg', $body);
continue;
}
}
if(!emptyempty($get_file) ){
if( mkdirs($img_path) )
{
$fp = fopen($img_path.$filename,"w");
if(fwrite($fp,$get_file)){
$body = preg_replace("/".addcslashes($value,"/")."/isu", $img_rpath.$filename, $body);
}
fclose($fp);
@sleep(6);
}
}
}
$body =str_replace("<img","<img ",$body);
return $body;
}
function mkdirs($dir)
{
if(!is_dir($dir)){
if(!mkdirs(dirname($dir))){
return false;}
if(!mkdir($dir,0777)){
return false;}
}
return true;
}
//用法如下:
$str ='fasfsdafsa<img src=http://filesimg.xxxx.com/2010/03/2010062300391582.jpg />';
echo get_remote($str,'圖片');
希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。
- PHP多線程批量采集下載美女圖片的實(shí)現(xiàn)代碼(續(xù))
- PHP批量采集下載圖片的實(shí)現(xiàn)代碼
- php curl簡(jiǎn)單采集圖片生成base64編碼(并附curl函數(shù)參數(shù)說(shuō)明)
- PHP CURL采集百度搜尋結(jié)果圖片不顯示問(wèn)題的解決方法
- PHP遠(yuǎn)程采集圖片詳細(xì)教程
- PHP采集類Snoopy抓取圖片實(shí)例
- phpphp圖片采集后按原路徑保存圖片示例
- php采集文章中的圖片獲取替換到本地(實(shí)現(xiàn)代碼)
- PHP 文章中的遠(yuǎn)程圖片采集到本地的代碼
- PHP下載采集圖片到本地的方法詳解【可忽略ssl認(rèn)證】
相關(guān)文章
PHP中file_exists與is_file,is_dir的區(qū)別介紹
很顯然file_exists是受了asp的影響,因?yàn)閍sp不但有fileExists還有folderExists,driverExists,那么PHP中file_exists是什么意思呢2012-09-09
PHP code 驗(yàn)證碼生成類定義和簡(jiǎn)單使用示例
這篇文章主要介紹了PHP code 驗(yàn)證碼生成類定義和簡(jiǎn)單使用,結(jié)合實(shí)例形式分析了PHP code 驗(yàn)證碼生成類的基本功能定義、簡(jiǎn)單使用方法及操作注意事項(xiàng),需要的朋友可以參考下2020-05-05
Erlang的運(yùn)算符(比較運(yùn)算符,數(shù)值運(yùn)算符,移位運(yùn)算符,邏輯運(yùn)算符)
如果要比較兩個(gè)數(shù),如果兩個(gè)數(shù)之間是不同的類型,比如float和int那么,==操作會(huì)首先把兩個(gè)數(shù)字轉(zhuǎn)換成相同的相同類型2012-07-07
Laravel框架中實(shí)現(xiàn)使用阿里云ACE緩存服務(wù)
這篇文章主要介紹了Laravel框架中實(shí)現(xiàn)使用阿里云ACE緩存服務(wù),本文擴(kuò)展了一個(gè)ACE緩存驅(qū)動(dòng),以便使用阿里云ACE緩存服務(wù),需要的朋友可以參考下2015-02-02
php實(shí)現(xiàn)接口api數(shù)據(jù)簽名及驗(yàn)簽
api數(shù)據(jù)簽名作用就是通過(guò)使用簽名可以驗(yàn)證數(shù)據(jù)在傳輸過(guò)程中是否被篡改或修改,下面小編就來(lái)為大家介紹一下php如何實(shí)現(xiàn)接口api數(shù)據(jù)簽名及驗(yàn)簽吧2023-11-11
php自定義函數(shù)實(shí)現(xiàn)JS的escape的方法示例
這篇文章主要介紹了php自定義函數(shù)實(shí)現(xiàn)JS的escape的方法,結(jié)合完整實(shí)例形式分析了php實(shí)現(xiàn)JS的escape功能函數(shù)的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07
php文件包含目錄配置open_basedir的使用與性能詳解
下面小編就為大家?guī)?lái)一篇php文件包含目錄配置open_basedir的使用與性能詳解。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04
編譯php 5.2.14+fpm+memcached(具體操作詳解)
本篇文章是對(duì)編譯php5.2.14+fpm+memcached的具體操作進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06

