PHP automatically collects pictures and saves remote pictures locally

From , 4 Years ago, written in PHP, viewed 219 times.
URL https://pastebin.vip/view/25ef0d88
  1. //自动采集内容中带有图片地址的远程图片 并保存到本地
  2. function my_file_get_contents($url, $timeout=30) {
  3.  if ( function_exists('curl_init') )
  4.  {
  5.   $ch = curl_init();
  6.   curl_setopt ($ch, curlopt_url, $url);
  7.   curl_setopt ($ch, curlopt_returntransfer, 1);
  8.   curl_setopt ($ch, curlopt_connecttimeout, $timeout);
  9.   $file_contents = curl_exec($ch);
  10.   curl_close($ch);
  11.  }
  12.  else if ( ini_get('allow_url_fopen') == 1 || strtolower(ini_get('allow_url_fopen')) == 'on' )  
  13.  {
  14.   $file_contents = @file_get_contents($url);
  15.  }
  16.  else
  17.  {
  18.   $file_contents = '';
  19.  }
  20.  return $file_contents;
  21. }
  22.  
  23.  
  24. function get_remote($body,$title){
  25.  $img_array = array();
  26.  $img_path = realpath("../../../upfile/news/").'/'.date("y/m/d/"); //采集远程图片保存地址
  27.  //die($img_path);
  28.  $img_rpath='/upfile/news/'.date("y/m/d/");  //设置访问地址
  29.  $body = stripslashes(strtolower($body));
  30.  preg_match_all("/(src|src)=["|'| ]{0,}(http://(.*).(gif|jpg|jpeg|png))/isu",$body,$img_array);
  31. $img_array = array_unique($img_array[2]);
  32. foreach ($img_array as $key => $value) {
  33.  $get_file = my_file_get_contents($value,60);
  34.  $filetime = time();  
  35.  $filename = date("ymdhis",$filetime).rand(1,999).'.'.substr($value,-3,3);
  36.  if(empty($get_file)){
  37.   @sleep(10);
  38.   $get_file = my_file_get_contents($value,30);
  39.   if(empty($get_file)){
  40.    $body = preg_replace("/".addcslashes($value,"/")."/isu", '/notfound.jpg', $body);
  41.    continue;
  42.    }
  43.  }
  44.  if(!empty($get_file) ){
  45.   if( mkdirs($img_path) )
  46.   {
  47.    $fp = fopen($img_path.$filename,"w");
  48.    if(fwrite($fp,$get_file)){        
  49.     $body = preg_replace("/".addcslashes($value,"/")."/isu", $img_rpath.$filename, $body);
  50.    }
  51.    fclose($fp);
  52.    @sleep(6);
  53.   }  
  54.  }    
  55.  
  56. }
  57. $body =str_replace("<img","<img ",$body);
  58. return $body;
  59.  
  60. }
  61. function mkdirs($dir)
  62. {
  63. if(!is_dir($dir)){
  64.  if(!mkdirs(dirname($dir))){
  65.   return false;}
  66.  if(!mkdir($dir,0777)){
  67.   return false;}
  68. }
  69. return true;
  70. }
  71. $str ='fasfsdafsa<img src=/get_pic/2014/01/10/10062300391582.jpg />';
  72. echo get_remote($str,'图片');

Reply to "PHP automatically collects pictures and saves remote pictures locally"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website