助力软件开发企业降本增效 PHP / java源码系统,只需一次付费,代码终身使用! 广告
>[info] 请到 `extra/qiniu.php` 配置你的七牛 `accessKey` 等配置信息,然后使用 `extend/Qiniu.php` 类轻松使用七牛上传文件 ##配置 `extra/qiniu.php` ``` return [ // 七牛云存储配置信息 "accessKey" => "", "secretKey" => "", "bucket" => "", // 存储空间 "domain" => "http://qiniu.tianpian.net.cn/", // 访问域名 ]; ``` ##实例化配置 实例化一个单例对象时传入的配置,`\Qiniu::instance($config = [])` ``` $config = [ 'mimes' => [], // 允许上传的文件 mime 类型 'max_size' => 0, // 上传的文件大小限制 (0-不做限制) 'exts' => [], // 允许上传的文件后缀,不包含点,例如 ['jpg', 'png', 'gif'] 'url' => "http://upload.qiniu.com/", // 上传的地址 'param' => [], // 参数 ]; ``` ##上传单个文件,文件直传 直接传入文件路径名上传文件 ###方法 `\Qiniu::instance($config = [])->uploadOne($file_path,$prefix="",$name=null,$token=null,$params = null, $mime = 'application/octet-stream', $checkCrc = false)` ###参数 | 名称 | 类型 | 说明 | | --- | --- | --- | | file_path | string | 要上传文件路径的全名 | | prefix | string | 随机字符串前缀,前后端统一随机字符串生成规则,请参考 [随机字符串](随机字符串) | | name | null \| string | 为空自动生成随机字符串,如果指定时需加上文件扩展名 | | token | null \| string | 为空自动获取 | | params | null \| array | 自定义变量,规则参考 [七牛 - 开发者指南](http://developer.qiniu.com/docs/v6/api/overview/up/response/vars.html#xvar) | | mime | string | 上传文件的 mime | | checkCrc | bool | 是否校验crc32 | ###使用示例 以下示例是百度编辑器 (Ueditor) 抓取远程图片到七牛云的方法 ``` /** * 远程图片抓取 */ public function remote() { $data = Request::instance()->param(); // 遍历上传 $ret["list"] = []; $upload = \Qiniu::instance(); $model = Loader::model("File"); foreach ($data['source'] as $source) { $file_name = basename($source); $file_path = TEMP_PATH . $file_name; // 远程下载图片到本地 $file_path = \File::downloadImage($source, $file_path, 1); // 上传图片 $info = $upload->uploadOne($file_path, "image/"); if ($error = $upload->getError()) { $ret['list'][] = ['state' => $error]; } else { // 上传成功,将数据写入到本地数据库中 $model->insertRecord($info, 1); $ret['list'][] = [ 'state' => "SUCCESS", 'url' => $info['key'], 'source' => $source, ]; } // 删除临时下载的文件 unlink($file_path); } return Response::create($ret, 'json'); } ``` >[info] 上传失败返回 false ,通过 `\Qiniu::instance()->getError()` 获取错误信息 ##多个文件上传 使用 ajax 或 file 控件直接上传文件,上传后带有 `$_FILES` 变量的上传 ###方法 `\Qiniu::instance()->upload($prefix="",$params = null, $checkCrc = false)` ###参数 | 名称 | 类型 | 说明 | | --- | --- | --- | | prefix | string | 随机字符串前缀,前后端统一随机字符串生成规则,请参考 [随机字符串](随机字符串) | | params | null \| array | 自定义变量,规则参考 [七牛 - 开发者指南](http://developer.qiniu.com/docs/v6/api/overview/up/response/vars.html#xvar) | | checkCrc | bool | 是否校验crc32 | ###使用示例 以下示例是百度编辑器 (Ueditor) 上传图片和文件到七牛云的方法 ``` /** * 图片上传 */ public function upload($prefix = "image/", $cate = 1) { $upload = \Qiniu::instance(); $info = $upload->upload($prefix); $error = $upload->getError(); // 保存图片到数据库 if ($info) { $model = Loader::model("File"); foreach ($info as $v) { if (is_array($v)) { $model->insertRecord($v, $cate); } } } if (!empty($error)) { $ret['state'] = $error; } else { $ret = [ "state" => 'SUCCESS', "url" => $info[0]['key'], "title" => $info[0]['name'], "original" => $info[0]['name'], "type" => $info[0]['type'], "size" => $info[0]['size'], ]; } return Response::create($ret, 'json'); } ``` >[info] 如果是有多个 `input[type='file']` 的控件上传,则返回的数据是: ``` $ret = [ 'key1' => [ ['key' => '上传到七牛的文件名', 'hash' => '文件hash值', 'name' => '原文件名', 'type' => '文件mime', 'ext' => '文件扩展名'], ['key' => '上传到七牛的文件名', 'hash' => '文件hash值', 'name' => '原文件名', 'type' => '文件mime', 'ext' => '文件扩展名'], ], 'key2' => [ ['key' => '上传到七牛的文件名', 'hash' => '文件hash值', 'name' => '原文件名', 'type' => '文件mime', 'ext' => '文件扩展名'], '错误信息', ], ]; ``` >[info] 如果出现错误,返回的文件信息数组的地方变成了错误信息,判断该位置是否是数组即可,可以通过 `\Qiniu::instance()->getError()` 获取最后一个错误信息 如果只有一个 `input[type='file']` 的控件上传,则返回的数据是: ~~~ $ret = [ ['key' => '上传到七牛的文件名', 'hash' => '文件hash值', 'name' => '原文件名', 'type' => '文件mime', 'ext' => '文件扩展名'], '错误信息', ]; ~~~ >[info] 返回的文件名为生成的随机字符串 ##获取七牛上传token 如果想直接获取 `token` 调用此方法即可 ###方法 直接返回 `token` `\Qiniu::token($expires = 3600, $bucket = null ,$key = null, $policy = null, $strictPolicy = true)` ###参数 | 名称 | 类型 | 说明 | | --- | --- | --- | | expires | int | token 过期时间,单位秒 | | bucket | string | 七牛对象存储 bucket ,为空则取配置信息里的 bucket | | key | mixed | key | | policy | mixed | policy | | strictPolicy | bool | strictPolicy | ###使用示例 以下示例是七牛上传类里面的方法 ``` /** * 上传单个文件,文件直传 * @param $file_path * @param string $prefix 文件名前缀,可以模拟目录 * @param null $name * @param null $token * @param null $params * @param string $mime * @param bool $checkCrc * @return bool */ public function uploadOne($file_path, $prefix = "", $name = null, $token = null, $params = null, $mime = 'application/octet-stream', $checkCrc = false) { if (empty($name)) $name = get_random($prefix) . "." . pathinfo($file_path, PATHINFO_EXTENSION); if (empty($token)) $token = self::token(); if (empty($mime)) $mime = 'application/octet-stream'; $this->error = null; list($ret, $error) = self::manager()->putFile($token, $name, $file_path, $params, $mime, $checkCrc); if ($error !== null) { $this->error = $error->message(); return false; } else { return $ret; } } ``` ##完美支持百度编辑器 (Ueditor) 在页面引入 `ueditor` 的 js 文件后,然后实例化 `ueditor` 时增加 `serverUrl` 配置即可 ``` // HTML 代码 <script id="editor" type="text/plain" style="height:400px"></script> // JavaScript 代码 <script>window.UEDITOR_HOME_URL = '__LIB__/ueditor/1.4.3/'</script> <script type="text/javascript" charset="utf-8" src="__LIB__/ueditor/1.4.3/ueditor.config.js"></script> <script type="text/javascript" charset="utf-8" src="__LIB__/ueditor/1.4.3/ueditor.all.min.js"> </script> <script type="text/javascript" charset="utf-8" src="__LIB__/ueditor/1.4.3/lang/zh-cn/zh-cn.js"></script> <script> $(function () { var ue = UE.getEditor('editor',{ serverUrl:'{:\\think\\Url::build("Ueditor/index")}' }); }) </script> ``` >[info] 具体使用方法和在线体验请参考 [在线体验 - 示例 - 百度编辑器](http://tpadmin.demo.tianpian.net.cn) 默认管理员帐号:admin,默认管理员密码:123456