php自动采集内容中带有图片地址的远程图片保存到本地
2013-09-05 19:25
831 查看
php自动采集内容中带有图片地址的远程图片保存到本地
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/"); //采集远程图片保存地址
//die($img_path);
$img_rpath='/upfile/news/'.date("y/m/d/"); //设置访问地址
$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(empty($get_file)){
@sleep(10);
$get_file = my_file_get_contents($value,30);
if(empty($get_file)){
$body = preg_replace("/".addcslashes($value,"/")."/isu", '/notfound.jpg', $body);
continue;
}
}
if(!empty($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=/get_pic/2010/03/2010062300391582.jpg />';
echo get_remote($str,'图片');
本站原创教程转请注明来源,文件上传代码会这样做。
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/"); //采集远程图片保存地址
//die($img_path);
$img_rpath='/upfile/news/'.date("y/m/d/"); //设置访问地址
$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(empty($get_file)){
@sleep(10);
$get_file = my_file_get_contents($value,30);
if(empty($get_file)){
$body = preg_replace("/".addcslashes($value,"/")."/isu", '/notfound.jpg', $body);
continue;
}
}
if(!empty($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=/get_pic/2010/03/2010062300391582.jpg />';
echo get_remote($str,'图片');
本站原创教程转请注明来源,文件上传代码会这样做。
相关文章推荐
- php采集内容中带有图片地址的远程图片并保存的方法
- php采集内容中带有图片地址的远程图片并保存的方法
- PHP采集远程图片保存本地
- php自动保存文章内容中的图片
- 用php获取远程图片并把它保存到本地的代码
- php 远程图片保存到本地的函数类
- php把采集内容中图片地址下载并替换成本地地址
- php获取远程图片并下载保存到本地的方法分析
- 转:php 保存远程图片到本地
- java下载远程http地址的图片文件到本地-自动处理图片是否经过服务器gzip压缩的问题
- php获取远程图片并把它保存到本地
- php 文章中的远程图片采集到本地
- 用asp自动解析网页中的图片地址,并将其保存到本地服务器
- php获取远程图片并下载保存到本地
- php实现异步将远程链接上内容(图片或内容)写到本地的方法
- PHP远程获取图片并保存到本地
- 远程图片自动保存到本地服务器
- PHP保存远程图片到本地的类
- 用asp自动解析网页中的图片地址,并将其保存到本地服务器
- php获取远程图片并把它保存到本地