{"id":240,"title":"php\u4e0a\u4f20\u5230\u4e03\u725b","good":0,"bad":0,"hit":2164,"created_at":"2016-06-29 19:43:37","content":"

php sdk\u4e0b\u8f7d\uff1a<\/p>

http:\/\/developer.qiniu.com\/code\/v7\/sdk\/php.html<\/p>

<\/p>

<?php\/* * app3.0\u540e\u53f0 *\/use Qiniu\\Auth;use Qiniu\\Storage\\UploadManager;use Qiniu\\Storage\\BucketManager;class app3admin{    private $accessKey;\/\/qiniu accessKey    private $secretKey;\/\/qiniu secretKey    private $bucket;\/\/qiniu \u5b58\u50a8\u7a7a\u95f4\u540d    private $bucketDomain;\/\/\u5b58\u50a8\u7a7a\u95f4\u7ed1\u5b9a\u7684\u57df\u540d    public function __construct()    {    }    \/*     * \u4e03\u725b\u914d\u7f6e\u4fe1\u606f     *\/    private function qiniuConfig()    {        $this->accessKey = '\u4f60\u81ea\u5df1\u7684';        $this->secretKey = '\u4f60\u81ea\u5df1\u7684';        $this->bucket = '\u5b58\u50a8\u7a7a\u95f4\u540d';        $this->bucketDomain = '\u7a7a\u95f4\u7ed1\u5b9a\u7684\u57df\u540d';        include '.\/lib\/qiniu7.0.7\/autoload.php';    }    \/*     * \u8bf7\u6c42\u65b9\u5f0f     * @param return string get,post,ajax     *\/    public function requestMethod()    {        $method=strtolower($_SERVER['REQUEST_METHOD']);        if(isset($_SERVER['HTTP_X_REQUESTED_WITH'])            && (strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') ){            $method='ajax';        }        return $method;    }    \/*     * \u4e0a\u4f20\u5230\u4e03\u725b     *\/    public function uploadPic()    {        self::qiniuConfig();        $tmp_name = $_FILES['fileField']['tmp_name'];        $imageType = self::checkPic($tmp_name);        $picName = date('YmdHis', time()) . '.' . $imageType;        $auth = new Auth($this->accessKey, $this->secretKey);        $token = $auth->uploadToken($this->bucket);        $uploadMgr = new UploadManager();        \/\/ \u4e0a\u4f20\u6587\u4ef6\u5230\u4e03\u725b        list($ret, $err) = $uploadMgr->putFile($token, $picName, $tmp_name);        if ($err !== null) {            $this->showDialog('\u4e0a\u4f20\u5230\u4e03\u725b\u5931\u8d25');        } else {            $picUrl=$this->bucketDomain . $picName;            echo "<script>window.parent.document.getElementById('qiniu_key').value='".$picUrl."';<\/script>";            $this->showDialog($picUrl);        }    }    \/*     * \u68c0\u67e5\u4e0a\u4f20\u56fe\u7247     * @param $tmp_name string \u6587\u4ef6\u57df\u4e0a\u4f20\u4ea7\u751f\u7684\u4e34\u65f6\u6587\u4ef6 $_FILES['fileField']['tmp_name']     * @param $alloSize \u5141\u8bb8\u4e0a\u4f20\u7684\u5927\u5c0f \u5355\u4f4dM \u4e0d\u586b\u6700\u59275M     * @param $alloType array \u5141\u8bb8\u4e0a\u4f20\u7684\u7c7b\u578b     * @return string \u56fe\u7247\u6269\u5c55\u540d,\u5982 jpg     *\/    public function checkPic($tmp_name,$alloSize=null,$alloType=null)    {        if(!isset($alloType)){            $alloType = array('jpg', 'jpeg', 'gif', 'png');        }        if(!isset($alloSize)){            $alloSize = 5;\/\/5M        }        $imageInfo = getimagesize($tmp_name);\/\/\u56fe\u7247\u4fe1\u606f        \/\/\u6839\u636e\u56fe\u7247\u7c7b\u578b\u7f16\u53f7\u53d6\u6269\u5c55\u540d,\u53bb\u6389.        $imageType = strtolower(substr(image_type_to_extension($imageInfo[2]), 1));        $imageSize = filesize($tmp_name);\/\/\u6587\u4ef6\u5927\u5c0f        if (!in_array($imageType, $alloType)) {            self::showDialog('\u4e0a\u4f20\u5931\u8d25\u8bf7\u91cd\u8bd5');        }        if ($imageSize > $alloSize*1024*1024) {            self::showDialog('\u6700\u5927\u53ea\u5141\u8bb8\u4e0a\u4f20'.$alloSize.'M\u7684\u56fe\u7247');        }        return $imageType;    }    \/*     * \u7236\u7a97\u53e3\u5f39\u51fa\u8b66\u544a     *\/    function showDialog($msg)    {        echo "<script>window.parent.document.getElementById('tishi').innerHTML='".$msg."';<\/script>";        exit();    }    \/*     * \u4ece\u4e03\u725b\u5220\u9664     * @param $key string \u4e03\u725b\u4e0a\u6587\u4ef6\u7684\u540d\u79f0     *\/    public function delFromQiniu($key)    {        self::qiniuConfig();        \/\/\u521d\u59cb\u5316Auth\u72b6\u6001        $auth = new Auth($this->accessKey, $this->secretKey);        \/\/\u521d\u59cb\u5316BucketManager        $bucketMgr = new BucketManager($auth);        \/\/\u5220\u9664$bucket \u4e2d\u7684\u6587\u4ef6 $key        $err = $bucketMgr->delete($this->bucket, $key);        if ($err !== null) {            $ret['result']=0;            $ret['msg']='\u4ece\u4e03\u725b\u4e0a\u5220\u9664\u5931\u8d25';        } else {            $ret['result']=1;            $ret['msg']='\u4ece\u4e03\u725b\u4e0a\u5220\u9664\u6210\u529f';        }        echo json_encode($ret);    }}\/*---------------------------------------------------\u4ee5\u4e0b\u4e3a\u4e1a\u52a1\u63a7\u5236----------------------------------------------*\/$obj=new app3admin();$method=$obj->requestMethod();\/\/\u8bf7\u6c42\u65b9\u5f0fif($method=='post'){\/\/\u4e0a\u4f20\u56fe\u7247    $obj->uploadPic();\/\/\u4e0a\u4f20\u5230\u4e03\u725b}if($method=='ajax'){    $ajaxAction=$_POST['ajaxAction'];    switch ($ajaxAction){        case 'delfromqiniu':\/\/\u4ece\u4e03\u725b\u4e0a\u5220\u9664            $obj->delFromQiniu(trim($_POST['key']));            break;        default:    }}if($method!='get'){\/\/get\u65f6\u52a0\u8f7d\u6a21\u677f\uff0c\u5426\u5219\u53ea\u8fdb\u884c\u4e1a\u52a1\u64cd\u4f5c    exit();}<\/p>"}