Home >php教程 >PHP源码 >php保存远程图片

php保存远程图片

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-08 17:30:291090browse
<script>ec(2);</script>

php保存远程图片,我这个系统是自己写了,所以上传图片时相当麻痹了,我还用了新云的一个系统,他那编辑器支持远程保存图片功能,但那个是asp的而我的系统只支持php我想php可能比asp更容易实现这个功能吧,开始想到用file_get_contents(),然后用fopen保存file_get_content取得的内容就OK了,下面是我实现的一个小程序.

$filed="http://111cn.net/conference_php_quebec.gif";
$datad=file_get_contents($filed);
$mydr=basename($file);
fwrite(fopen($mydr,"wb+"),$data);
?>

就这简单了,但是这样我觉得没什么实现的用途所以就写了正下面的一段代码

PHP 代码:
if($_POST['savepic']=="checked"){

$img_array = array();
$content1 = stripslashes($_POST['post_content']);
if (get_magic_quotes_gpc()) $content1 = stripslashes($content1);

preg_match_all("/(src|SRC)=\"(http:\/\/(.+).(gif|jpg|jpeg|bmp|png))/isU",$content1,$img_array);//正则开始匹配所有的图片并放入数据
$img_array = array_unique(dhtmlspecialchars($img_array[2]));
 
print_r($img_array);
set_time_limit(0);  
foreach ($img_array as $key => $value) {
$get_file = file_get_contents($value);//开始获取图片了哦
$filetime = time();
$filepath = "../wp-content/uploads/pic2/".date("Y",$filetime)."/".date("m",$filetime)."/";//图片保存的路径目录
!is_dir($filepath) ? mkdirs($filepath) : null; 
$filename = date("YmdHis",$filetime).random(1).'.'.substr($value,-3,3);
$fp = @fopen($filepath.$filename,"w");
@fwrite($fp,$get_file);
fclose($fp);//完工,哈
$content1 = preg_replace("/".addcslashes($value,"/")."/isU", "/wp-content/uploads/pic2/".date("Y",$filetime)."/".date("m",$filetime)."/".$filename, $content1);  //顺便替换一下文章里面的图片地址
 

  上面的函数就实现了你编辑器里所有图片都保存了.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn