Php collects and saves remote images with image addresses. This article describes how to save and remotely collect images with image addresses in php, attackers can collect and save remote images with image addresses in php.
This article describes how to collect and save remote images with image addresses in php. it is a very practical technique to collect and save remote images, for more information, see
This example describes how to save and remotely collect images with image addresses in php. Share it with you for your reference. The specific implementation method is as follows:
The code is as follows:
Function my_file_get_contents ($ url, $ timeout = 30 ){
If (function_exists ('curl _ init '))
{
$ Ch = curl_init ();
Curl_setopt ($ ch, curlopt_url, $ url );
Curl_setopt ($ ch, curlopt_returntransfer, 1 );
Curl_setopt ($ ch, curlopt_connecttimeout, $ timeout );
$ File_contents = curl_exec ($ ch );
Curl_close ($ ch );
}
Else if (ini_get ('allow _ url_fopen') = 1 | strtolower (ini_get ('allow _ url_fopen') = 'on ')
{
$ File_contents = @ file_get_contents ($ url );
}
Else
{
$ File_contents = '';
}
Return $ file_contents;
}
The code is as follows:
Function get_remote ($ body, $ title ){
$ Img_array = array ();
$ Img_path = realpath (".. /.. /.. /upfile/news /"). '/'. date ("y/m/d/"); // address for storing remote images
// Die ($ img_path );
$ Img_rpath = '/upfile/news/'. date ("y/m/d/"); // Set the access address
$ Body = stripslashes (strtolower ($ body ));
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 = my_file_get_contents ($ value, 60 );
$ Filetime = time ();
$ Filename = date ("ymdhis", $ filetime). rand (1,999). '.'. substr ($ value );
If (emptyempty ($ get_file )){
@ Sleep (10 );
$ Get_file = my_file_get_contents ($ value, 30 );
If (emptyempty ($ get_file )){
$ Body = preg_replace ("/". addcslashes ($ value, "/"). "/isu", '/notfound.jpg', $ body );
Continue;
}
}
If (! Emptyempty ($ get_file )){
If (mkdirs ($ img_path ))
{
$ Fp = fopen ($ img_path. $ filename, "w ");
If (fwrite ($ fp, $ get_file )){
$ Body = preg_replace ("/". addcslashes ($ value, "/"). "/isu", $ img_rpath. $ filename, $ body );
}
Fclose ($ fp );
@ Sleep (6 );
}
}
}
$ Body = str_replace ("return $ body;
}
Function mkdirs ($ dir)
{
If (! Is_dir ($ dir )){
If (! Mkdirs (dirname ($ dir ))){
Return false ;}
If (! Mkdir ($ dir, 0777 )){
Return false ;}
}
Return true;
}
// Usage:
$ Str = 'fasfsdafsa ';
Echo get_remote ($ str, 'image ');
I hope this article will help you with php programming.
This article describes how to save and collect remote images with image addresses in php...