Home > Backend Development > PHP Tutorial > Save remote pictures in php_PHP tutorial

Save remote pictures in php_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-13 17:07:10
Original
986 people have browsed it

php saves remote images. The following code is written by myself, but one problem is that the success of saving images on the server is not very high. I use file_get_contents to obtain them. Finally, I found that if the host has anti-theft I read

php to save remote images. The following code was written by myself, but one problem is that the success of saving images when placed on the server is not very high. I used file_get_contents to obtain it, and finally found that if If the host is anti-theft, it will be read. The way we deceive the host is to use fsockopen to send a user packet to the host

function get_remote($body){
set_time_limit(0);
$img_array = array();
$img_path = "get_pic/";
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 = @file_get_contents($value);
$filetime = time();
!is_dir($img_path) ? mkdir($img_path) : null;
$filename = date("YmdHis",$filetime).rand(1,999).'.'.substr($value,-3,3);
if(empty($get_file)){
sleep(3);
$get_file = @file_get_contents($value);
If(empty($get_file)){
$body = preg_replace("/".addcslashes($value,"/")."/isU", 'http://www.111cn.cn/notfound.jpg', $body);
Continue;
}
}
if(!empty($get_file) ){
$fp = fopen($img_path.$filename,"w");
If(fwrite($fp,$get_file)){                                        $body = preg_replace("/".addcslashes($value,"/")."/isU", $img_path.$filename, $body);
}
}
fclose($fp);
sleep(1);
}

//$body =str_replace(' //$body =str_replace(' return $body;

}

Let’s take a look at the fsockopen method.

if($gcookie=="" && !empty($rfurl)) $gcookie = RefurlCookie($rfurl);

$sessionQuery = "GET $gquery HTTP/1.1rn";
$sessionQuery .= "Host: $ghostrn";
$sessionQuery .= "Referer: $rfurlrn";
$sessionQuery .= "Accept: */*rn";
$sessionQuery .= "User-Agent: Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)rn";
if($gcookie!=""&&!ereg("[rn]",$gcookie)) $sessionQuery .= $gcookie."rn";
$sessionQuery .= "Connection: Keep-Alivernrn";
$errno = "";
$errstr = "";
$url ='http://www.moko.cc';
$m_fp = fsockopen($url,80,$errno,$errstr,10);
fwrite($m_fp,$sessionQuery);


function RefurlCookie($gurl){
 global $gcookie,$lastRfurl;
 $gurl = trim($gurl);
 if(!empty($gcookie) && $lastRfurl==$gurl) return $gcookie;
 else $lastRfurl=$gurl;
 if(trim($gurl)=='') return '';
 $urlinfos = GetHostInfo($gurl);
  $ghost = $urlinfos['host'];
  $gquery = $urlinfos['query'];
  $sessionQuery = "GET $gquery HTTP/1.1rn";
  $sessionQuery .= "Host: $ghostrn";
  $sessionQuery .= "Accept: */*rn";
  $sessionQuery .= "User-Agent: Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)rn";
  $sessionQuery .= "Connection: Closernrn";
  $errno = "";
  $errstr = "";
  $m_fp = fsockopen($ghost, 80, $errno, $errstr,10) or die($ghost.'
');
  fwrite($m_fp,$sessionQuery);
  $lnum = 0;
  //获取详细应答头
  $gcookie = "";
 while(!feof($m_fp)){
   $line = trim(fgets($m_fp,256));
   if($line == "" || $lnum>100) break;
   else{
    if(eregi("^cookie",$line)){
     $gcookie = $line;
     break;
    }
   }
  }
   fclose($m_fp);
   }
  哈这样的话就可以保存远程有图片防盗的主机的图片了喽.

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/630485.htmlTechArticlephp保存远程图片,下面这段代码是我自己写的,但是有一个问题就是放在服务器就保存图片的成功不是很高,我用的是file_get_contents来取得,最后...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template