Php automatically collects remote images with image addresses and saves them to a local file. _ PHP Tutorial

Source: Internet
Author: User
Tags filetime
Php automatically collects remote images with image addresses and saves them to the local device. Php automatically collects remote images with image addresses and saves them to the local functionmy_file_get_contents ($ url, $ timeout30) {if (function_exists (curl_init) {$ chcurl_init (); php automatically collects remote images with image addresses and saves them to the local device.

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;
}


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 (empty ($ get_file )){
@ Sleep (10 );
$ Get_file = my_file_get_contents ($ value, 30 );
If (empty ($ get_file )){
$ Body = preg_replace ("/". addcslashes ($ value, "/"). "/isu", '/notfound.jpg', $ body );
Continue;
}
}
If (! Empty ($ 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;
}

$ Str = 'fasfsdafsa ';
Echo get_remote ($ str, 'image ');

Please refer to the source for the original tutorial on this site. the file upload code will do this.


Using function my_file_get_contents ($ url, $ timeout = 30) {if (function_exists ('curl _ init ') {$ ch = curl_init ();...

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.