// +---------------------------------------------------------------------- namespace Admin\Controller; use Think\Upload\Driver\Qiniu\QiniuStorage; /** * 七牛扩展类测试控制器 * @author yangweijie */ class QiniuController extends AdminController { public function _initialize(){ $config = array( 'accessKey'=>'__ODsglZwwjRJNZHAu7vtcEf-zgIxdQAY-QqVrZD', 'secrectKey'=>'Z9-RahGtXhKeTUYy9WCnLbQ98ZuZ_paiaoBjByKv', 'bucket'=>'blackwhite', 'domain'=>'blackwhite.u.qiniudn.com' ); $this->qiniu = new QiniuStorage($config); parent:: _initialize(); } //获取文件列表 public function index(){ $this->meta_title = '七牛云存储测试'; $map = array(); $prefix = trim(I('post.prefix')); if($prefix) $map['prefix'] = $prefix; $list = $this->qiniu->getList($map); if(!$list) trace($this->qiniu->error); $this->assign('qiniu', $this->qiniu); $this->assign('_list', $list['items']); $this->display(); } public function del(){ $file = trim(I('file')); if($file){ $result = $this->qiniu->del($file); if(false === $result){ $this->error($this->qiniu->errorStr); }else{ $this->success('删除成功'); } }else{ $this->error('错误的文件名'); } } public function dealImage($key){ $url = $this->qiniu->dealWithType($key, 'img') ; redirect($url); } public function dealDoc($key){ $url = $this->qiniu->dealWithType($key, 'doc'); redirect($url); } public function rename(){ $key = I('get.file'); $new = I('new_name'); $result = $this->qiniu->rename($key, $new); if(false === $result){ trace($this->qiniu->error); $this->error($this->qiniu->errorStr); }else{ $this->success('改名成功'); } } public function batchDel(){ $files = $_GET['key']; if(is_array($files) && $files !== array()){ $files = array_column($files,'value'); $result = $this->qiniu->delBatch($files); if(false === $result){ $this->error($this->qiniu->errorStr); }else{ $this->success('删除成功'); } }else{ $this->error('请至少选择一个文件'); } } public function detail($key){ $result = $this->qiniu->info($key); if($result){ if(in_array($result['mimeType'], array('image/jpeg','image/png'))){ $img = ""; }else{ $img = ''; } $time = date('Y-m-d H:i:s', bcmul(substr(strval($result['putTime']), 0, 11),"1000000000")); $filesize = format_bytes($result['fsize']); $tpl = << {$key}
{$img}

外链地址:

最后更新时间:{$time}

文件大小:{$filesize}

tpl; $this->success('as', '', array('tpl'=>$tpl)); }else{ $this->error('获取文件信息失败'); } } //上传单个文件 用uploadify public function uploadOne(){ $file = $_FILES['qiniu_file']; $file = array( 'name'=>'file', 'fileName'=>$file['name'], 'fileBody'=>file_get_contents($file['tmp_name']) ); $config = array(); $result = $this->qiniu->upload($config, $file); if($result){ $this->success('上传成功','', $result); }else{ $this->error('上传失败','', array( 'error'=>$this->qiniu->error, 'errorStr'=>$this->qiniu->errorStr )); } exit; } }