Home >php教程 >php手册 >thinkphp 七牛存储单文件、多文件上传,简化配置

thinkphp 七牛存储单文件、多文件上传,简化配置

WBOY
WBOYOriginal
2016-06-07 11:37:041034browse

简化使用七牛云存储实现文件上传功能
<?php <br /> <br> /**<br>  * 上传七牛<br>  *<br>  * @author huqitao <huqitaoit><br>  */<br> <br> namespace Common\Utility;<br> <br> class UploadImg {<br> <br>     protected $config;<br>     protected $domain;<br>     protected $bucket;<br> <br>     public function __construct($domain, $bucket) {<br>         $this->domain = $domain;<br>         $this->bucket = $bucket;<br>         $this->config = array(<br>             'maxSize' => 2 * 1024 * 1024, //文件大小<br>             'rootPath' => './',<br>             'saveName' => array('uniqid', ''),<br>             'driver' => 'Qiniu',<br>             'driverConfig' => array(<br>                 'secretKey' => '######',  //七牛空间配置参数<br>                 'accessKey' => '########',<br>                 'domain' => $this->domain, //空间地址<br>                 'bucket' => $this->bucket, //空间名称<br>             )<br>         );<br>     }<br> <br>   <br> <br>     /**<br>      * 上传一个文件<br>      * @param array $file 文件参数<br>      * @return array 返回 code 与文件路径或错误信息<br>      */<br>     public function uploadOne($file) {<br>         $upload = new \Think\Upload($this->config);<br>         $info = $upload->uploadOne($file);<br>         if (!$info) {<br>             return [550, $upload->getError()];<br>         }<br>         return [200, $info['url']]; <br>     }<br>     /**<br>      * 上传多图<br>      * @param type $files<br>      * @return array 返回 code 与文件路径数组 或错误信息<br>      */<br>     public function uploads($files) {<br>         $upload = new \Think\Upload($this->config);<br>         $info = $upload->upload($files);<br>         if (!$info) {<br>             return [550, $upload->getError()];<br>         }<br>         foreach ($info as $v) {<br>             $pArray[] = "http://".$this->domain."/".strtr($v['name'], '/', '_');<br>         }<br>         return [200, $pArray];<br>     }<br> <br> }</huqitaoit>使用方法,单图上传:public function uploadSchool() {<br>         if (IS_POST) {<br>             $domain = "#####";<br>             $bucket = "###";<br>             if (empty($_FILES)) {<br>                 $this->ajaxReturn(makeinformation(550, "没有上传图片"));<br>             }<br>             $uploadImg = new \Common\Utility\UploadImg($domain, $bucket);<br>             $data = $uploadImg->uploadOne($_FILES['file']);<br>             if ($data[0] != 200) {<br>                 $this->ajaxReturn(makeinformation(550, $data[1]));//失败返回错误<br>             }<br>             $this->ajaxReturn(makeinformation(200, '', array('url' => $data[1]))); //成功返回图片绝对地址<br>         }<br>     }

AD:真正免费,域名+虚机+企业邮箱=0元

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