您有个知识币

【退出】

php获取远程图片并保存到本地

php获取远程图片并保存到本地,这是一个比较通用的功能。

以下函数也写的比较通用,只需要传入一段html代码。

php获取远程图片并保存到本地,这个函数一般应用在编辑器和采集器中。

function auto_save_image($body){

$img_array = array();

preg_match_all("/(src)=[\"|\'| ]{0,}(http:\/\/(.*)\.(gif|jpg|jpeg|bmp|png))[\"|\'| ]{0,}/isU", $body, $img_array);

$img_array = array_unique($img_array[2]);

set_time_limit(0);

$imgPath = "uploadFiles/".date("Ym")."/";

$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);

if($get_file)

{

$fp = @fopen($rndFileName,"w");

@fwrite($fp,$get_file);

@fclose($fp);

}

$body = ereg_replace($value, $rndFileName, $body);

}

return $body;

}

本文固定链接: http://www.webzhishi.com/php-local-pic/ | web知识网

php获取远程图片并保存到本地:目前有1 条留言

  1. vsBeta.com:  

    很基础,很有用

    2012-05-17 08:23:13 [回复]

发表评论