Php saves remote images. Php saves the remote image. the following code is written by myself. However, the success of saving the image on the server is not very high. I used file_get_contents to retrieve the image, at last, php saved the remote image. the following code was written by myself. However, the success of saving the image on the server was not very high. I used file_get_contents to retrieve the image, finally, it is found that if the host has an anti-theft, it will be read.
Php saves the remote image. the following code is written by myself. However, the success of saving the image on the server is not very high. I used file_get_contents to retrieve the image, finally, if the host has an anti-theft, it will be read. we will 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 );
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;
// Obtain the detailed response 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 );
}
In this way, you can save images of remote hosts with image theft.
...