Function class for saving php remote images to a local device .? PhpFunction: get the remote image and save it locally. Make sure you have the permission to write the file to the local server. description: $ url is the complete U of the remote image. //
// Function: obtain the remote image and save it to the local device.
//
//
// Make sure you have the permission to write files to the local server
//
//
// Variable description:
// $ Url is the complete URL of the remote image. it cannot be blank.
// $ Filename is an optional variable. if it is null, the local file name is based on the time and date.
// Automatically generated.
Function GrabImage ($ url, $ filename = ""){
If ($ url = ""): return false; endif;
If ($ filename = ""){
$ Ext = strrchr ($ url ,".");
If ($ ext! = ". Gif" & $ ext! = ". Jpg"): return false; endif;
$ Filename = date ("dMYHis"). $ ext;
}
Ob_start ();
Readfile ($ url );
$ Img = ob_get_contents ();
Ob_end_clean ();
$ Size = strlen ($ img );
$ Fp2 = @ fopen ($ filename, "");
Fwrite ($ fp2, $ img );
Fclose ($ fp2 );
Return $ filename;
}
$ Img = GrabImage ("/upload/20081208002810980.jpg ","");
If ($ img): echo'
'; Else: echo "false"; endif;
?> In dedecms: if (! Empty ($ saveremoteimg ))
{
$ Body = stripslashes ($ body );
$ Img_array = array ();
Preg_match_all ("/(src | SRC) = [\" | '|] {0,} (http :\/\/(. *)\. (gif | jpg | jpeg | bmp | png)/isU ", $ body, $ img_array );
$ Img_array = array_unique ($ img_array [2]);
Set_time_limit (0 );
$ ImgUrl = $ img_dir. "/". strftime ("% Y % m % d", time ());
$ ImgPath = $ base_dir. $ imgUrl;
$ MilliSecond = strftime ("% H % M % S", time ());
If (! Is_dir ($ imgPath) @ mkdir ($ imgPath, 0777 );
Foreach ($ img_array as $ key => $ value)
{
$ Value = trim ($ value );
$ Get_file = @ file_get_contents ($ value );
$ RndFileName = $ imgPath. "/". $ milliSecond. $ key. ".". substr ($ value,-3, 3 );
$ Fileurl = $ imgUrl. "/". $ milliSecond. $ key. ".". substr ($ value,-3, 3 );
If ($ get_file)
{
$ Fp = @ fopen ($ rndFileName, "w ");
@ Fwrite ($ fp, $ get_file );
@ Fclose ($ fp );
}
$ Body = ereg_replace ($ value, $ fileurl, $ body );
}
$ Body = addslashes ($ body );
}
Http://www.bkjia.com/PHPjc/319705.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/319705.htmlTechArticle? Php /// Function: obtain the remote image and save it to the local device. //// make sure you have the permission to write the file to the local server. //// variable description: // $ url is the complete U of the remote image...