You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
3.2 KiB

3 years ago
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: yangweijie <yangweijiester@gmail.com> <http://www.code-tech.diandian.com>
  10. // +----------------------------------------------------------------------
  11. namespace Think\Upload\Driver;
  12. use Think\Upload\Driver\Qiniu\QiniuStorage;
  13. class Qiniu{
  14. /**
  15. * 上传文件根目录
  16. * @var string
  17. */
  18. private $rootPath;
  19. /**
  20. * 上传错误信息
  21. * @var string
  22. */
  23. private $error = '';
  24. private $config = array(
  25. 'secrectKey' => '', //七牛服务器
  26. 'accessKey' => '', //七牛用户
  27. 'domain' => '', //七牛密码
  28. 'bucket' => '', //空间名称
  29. 'timeout' => 300, //超时时间
  30. );
  31. /**
  32. * 构造函数,用于设置上传根路径
  33. * @param array $config FTP配置
  34. */
  35. public function __construct($config){
  36. $this->config = array_merge($this->config, $config);
  37. /* 设置根目录 */
  38. $this->qiniu = new QiniuStorage($config);
  39. }
  40. /**
  41. * 检测上传根目录(七牛上传时支持自动创建目录,直接返回)
  42. * @param string $rootpath 根目录
  43. * @return boolean true-检测通过,false-检测失败
  44. */
  45. public function checkRootPath($rootpath){
  46. $this->rootPath = trim($rootpath, './') . '/';
  47. return true;
  48. }
  49. /**
  50. * 检测上传目录(七牛上传时支持自动创建目录,直接返回)
  51. * @param string $savepath 上传目录
  52. * @return boolean 检测结果,true-通过,false-失败
  53. */
  54. public function checkSavePath($savepath){
  55. return true;
  56. }
  57. /**
  58. * 创建文件夹 (七牛上传时支持自动创建目录,直接返回)
  59. * @param string $savepath 目录名称
  60. * @return boolean true-创建成功,false-创建失败
  61. */
  62. public function mkdir($savepath){
  63. return true;
  64. }
  65. /**
  66. * 保存指定文件
  67. * @param array $file 保存的文件信息
  68. * @param boolean $replace 同名文件是否覆盖
  69. * @return boolean 保存状态,true-成功,false-失败
  70. */
  71. public function save(&$file,$replace=true) {
  72. $file['name'] = $file['savepath'] . $file['savename'];
  73. $key = str_replace('/', '_', $file['name']);
  74. $upfile = array(
  75. 'name'=>'file',
  76. 'fileName'=>$key,
  77. 'fileBody'=>file_get_contents($file['tmp_name'])
  78. );
  79. $config = array();
  80. $result = $this->qiniu->upload($config, $upfile);
  81. $url = $this->qiniu->downlink($key);
  82. $file['url'] = $url;
  83. return false ===$result ? false : true;
  84. }
  85. /**
  86. * 获取最后一次上传错误信息
  87. * @return string 错误信息
  88. */
  89. public function getError(){
  90. return $this->qiniu->errorStr;
  91. }
  92. }