PHP Save remote picture _php tutorial

Source: Internet
Author: User
Tags filetime
PHP Save remote picture, the following code is my own writing, but there is a problem is placed on the server to save the picture success is not very high, I use is file_get_contents to obtain, finally found if the host has anti-theft of the study took the

PHP Save remote picture, the following code is my own writing, but there is a problem is placed on the server to save the picture success is not very high, I use is file_get_contents to obtain, finally found if the host has anti-theft of the study took, we deceive the host method to do, is to use Fsockopen to send a user package 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 Fsockopen's 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, $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, $errno, $errstr, ten) or Die ($ghost. '
');
Fwrite ($m _fp, $sessionQuery);
$lnum = 0;
Get detailed answer Header
$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);
}
Ha such words will be able to save the remote have pictures of anti-theft host pictures.

http://www.bkjia.com/PHPjc/630485.html www.bkjia.com true http://www.bkjia.com/PHPjc/630485.html techarticle php Save remote Pictures, the following code is my own writing, but there is a problem is placed on the server to save the picture of success is not very high, I use the file_get_contents to obtain, finally ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.