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 ();...