php采集內(nèi)容中帶有圖片地址的遠(yuǎn)程圖片并保存的方法_PHP教程
教程Tag:暫無Tag,歡迎添加,賺取U幣!
推薦:php中socket通信機(jī)制實例詳解這篇文章主要介紹了php中socket通信機(jī)制,講述了socket通信機(jī)制的原理,并以實例形式較為詳細(xì)的分析了socket通信機(jī)制的用法,需要的朋友可以參考下 本文實例講述了php中socket通信機(jī)制及用法。分享給大家供大家參考。具體分析如下: 一、socket是什么 什么是socket 所謂so
這篇文章主要介紹了php采集內(nèi)容中帶有圖片地址的遠(yuǎn)程圖片并保存的方法,可實現(xiàn)采集并保存遠(yuǎn)程圖片的功能,是非常實用的技巧,需要的朋友可以參考下
本文實例講述了php采集內(nèi)容中帶有圖片地址的遠(yuǎn)程圖片并保存的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
代碼如下: function my_file_get_contents($url, $timeout=30) {
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;
}
代碼如下: function get_remote($body,$title){
$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è)置訪問地址
$body = stripslashes(strtolower($body));
$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,'圖片');
希望本文所述對大家的php程序設(shè)計有所幫助。
分享:php讀取csv數(shù)據(jù)保存到數(shù)組的方法這篇文章主要介紹了php讀取csv數(shù)據(jù)保存到數(shù)組的方法,通過封裝的類文件實現(xiàn)這一功能,是對csv文件操作的實用技巧,需要的朋友可以參考下 本文實例講述了php讀取csv數(shù)據(jù)保存到數(shù)組的方法。分享給大家供大家參考。具體分析如下: csv是常用的excel格式的替代品,很多時候我們
相關(guān)PHP教程:
- php中socket通信機(jī)制實例詳解
- php讀取csv數(shù)據(jù)保存到數(shù)組的方法
- php獲取用戶瀏覽器版本的方法
- php堆排序?qū)崿F(xiàn)原理與應(yīng)用方法
- php購物車實現(xiàn)方法
- PHP實現(xiàn)格式化文件數(shù)據(jù)大小顯示的方法
- php自定義加密與解密程序?qū)嵗?/a>
- php使用google地圖應(yīng)用實例
- php將文本文件轉(zhuǎn)換csv輸出的方法
- 19個Android常用工具類匯總
- php+ajax實現(xiàn)文章自動保存的方法
- php實現(xiàn)監(jiān)控varnish緩存服務(wù)器的狀態(tài)
- 相關(guān)鏈接:
- 教程說明:
PHP教程-php采集內(nèi)容中帶有圖片地址的遠(yuǎn)程圖片并保存的方法
。