Save remote pictures in php_PHP tutorial
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;
}
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);
}
哈这样的话就可以保存远程有图片防盗的主机的图片了喽.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
