Qiniu.class.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. * 上传文件根目录
  17. * @var string
  18. */
  19. private $rootPath;
  20. /**
  21. * 上传错误信息
  22. * @var string
  23. */
  24. private $error = '';
  25. private $config = array(
  26. 'secretKey' => '', //七牛服务器
  27. 'accessKey' => '', //七牛用户
  28. 'domain' => '', //七牛密码
  29. 'bucket' => '', //空间名称
  30. 'timeout' => 300, //超时时间
  31. );
  32. /**
  33. * 构造函数,用于设置上传根路径
  34. * @param array $config FTP配置
  35. */
  36. public function __construct($config)
  37. {
  38. $this->config = array_merge($this->config, $config);
  39. /* 设置根目录 */
  40. $this->qiniu = new QiniuStorage($config);
  41. }
  42. /**
  43. * 检测上传根目录(七牛上传时支持自动创建目录,直接返回)
  44. * @param string $rootpath 根目录
  45. * @return boolean true-检测通过,false-检测失败
  46. */
  47. public function checkRootPath($rootpath)
  48. {
  49. $this->rootPath = trim($rootpath, './') . '/';
  50. return true;
  51. }
  52. /**
  53. * 检测上传目录(七牛上传时支持自动创建目录,直接返回)
  54. * @param string $savepath 上传目录
  55. * @return boolean 检测结果,true-通过,false-失败
  56. */
  57. public function checkSavePath($savepath)
  58. {
  59. return true;
  60. }
  61. /**
  62. * 创建文件夹 (七牛上传时支持自动创建目录,直接返回)
  63. * @param string $savepath 目录名称
  64. * @return boolean true-创建成功,false-创建失败
  65. */
  66. public function mkdir($savepath)
  67. {
  68. return true;
  69. }
  70. /**
  71. * 保存指定文件
  72. * @param array $file 保存的文件信息
  73. * @param boolean $replace 同名文件是否覆盖
  74. * @return boolean 保存状态,true-成功,false-失败
  75. */
  76. public function save(&$file, $replace = true)
  77. {
  78. $file['name'] = $file['savepath'] . $file['savename'];
  79. $key = str_replace('/', '_', $file['name']);
  80. $upfile = array(
  81. 'name' => 'file',
  82. 'fileName' => $key,
  83. 'fileBody' => file_get_contents($file['tmp_name']),
  84. );
  85. $config = array();
  86. $result = $this->qiniu->upload($config, $upfile);
  87. $url = $this->qiniu->downlink($key);
  88. $file['url'] = $url;
  89. return false === $result ? false : true;
  90. }
  91. /**
  92. * 获取最后一次上传错误信息
  93. * @return string 错误信息
  94. */
  95. public function getError()
  96. {
  97. return $this->qiniu->errorStr;
  98. }
  99. }