Remote images with image addresses in PHP auto-capture content are saved locally
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/"); Capture Remote picture Save address
Die ($img _path);
$img _rpath= '/upfile/news/'. Date ("y/m/d/"); Set 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, -3,3);
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, ' pictures ');
This site original tutorial transfer please indicate the source, the file upload code will do so.
http://www.bkjia.com/PHPjc/444916.html www.bkjia.com true http://www.bkjia.com/PHPjc/444916.html techarticle The remote image with the image address in the PHP auto-capture content is saved to local function my_file_get_contents ($url, $timeout =30) {if (function_exists (' Curl_init ')) { $ch = Curl_init (); ..