How to save a remote image with an image address in the content collected by php _ PHP Tutorial

Source: Internet
Author: User
Tags filetime
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...

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.