php后台添加文章时,直接下载远程图片php

/ / 2016-01-29   阅读:2517
<?php     $str = 'http://www.thinkphp.cn/Uploads/info/2013-07-24/51ef32f4490e3_100_100.jpg&http://www.thinkphp.cn/Uploads/ad/2013-05-09/518b1b272d448.jpg&http://www.thinkphp.cn/U...
<?php
    $str = 'http://www.thinkphp.cn/Uploads/info/2013-07-24/51ef32f4490e3_100_100.jpg&http://www.thinkphp.cn/Uploads/ad/2013-05-09/518b1b272d448.jpg&http://www.thinkphp.cn/Uploads/ad/2013-07-02/51d24ce472da0.jpg&http://www.thinkphp.cn/Uploads/ad/2013-07-04/51d4c7c4aa490.jpg';

    auto_save_image($str);

    function auto_save_image($body){
        $img_array = explode('&',$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]);*/ //用于对img标签自动匹配
        set_time_limit(0);
        $imgPath = "uploads/allimg/".date("Ymd")."/";
        $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;
    }

我要评论

昵称:
验证码:

最新评论

共0条 共0页 10条/页 首页 上一页 下一页 尾页
意见反馈