oss之Kissy编辑器上传云端代码

jerry thinkphp 2015年11月19日 收藏
针对阿里kissy编辑器上传图片到阿里云oss存储服务器代码
直接上干货
    public function index()
    {
        $this->savePathNew = C('ADMIN_UPLOAD_DIR').'Article/' ;
        $this->saveRule = date("YmdHis",time());
        $info = $this->CUpload();
        $image_url = $info[0]['savepath'].$info[0]['savename'];
        //上传成功
        if($image_url){
            $returnul = $this->upload_file_to_alioss($image_url);
            if($returnul){
                echo '{"status": "0", "imgUrl": "' .$returnul. '"}';
            }else{
                echo '{"status": "1", "error": "'.$info['info'].'"}';
            }
            //echo '{"status": "0", "imgUrl": "' .__APP__."/".$image_url. '"}';
        }else{
            echo '{"status": "1", "error": "'.$info['info'].'"}';
        }
    }

    //uploader 插件上传图片到oss
    function upload_file_to_alioss($image_url)
    {
        $ossinfo = C("OSS_SERVERINFO");
        import("ORG.Alioss.alioss");
        $oss = new ALIOSS($access_id = $ossinfo['OSS_ACCESS_ID'], $access_key = $ossinfo['OSS_ACCESS_KEY'], $hostname = $ossinfo['OSS_ENDPOINT'], $security_token = NULL);
        $bucket = $ossinfo['OSS_TEST_BUCKET'];
        $picname = 'borrow-borrow_info-'. time() . rand(111111, 999999).'.jpg';
        $object = 'images/uploads/'.$picname;
        $file_path = DOCUMENT_ROOT.'/'.$image_url;
        $options = array();
        $res = $oss->upload_file_by_file($bucket, $object, $file_path, $options);
        Log::write('image_url======'.json_encode($res), Log::SQL);//查看调试信息
        if ($res->status != 200) {
            return false;
        }else{
            Log::write('远程url链接======'.$ossinfo['OOS_MYPOINT'].$object, Log::SQL);
            return $ossinfo['OOS_MYPOINT'].$object;
        }
    }