Sae.class.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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: luofei614<weibo.com/luofei614>
  10. // +----------------------------------------------------------------------
  11. namespace Think\Upload\Driver;
  12. class Sae
  13. {
  14. /**
  15. * Storage的Domain
  16. * @var string
  17. */
  18. private $domain = '';
  19. private $rootPath = '';
  20. /**
  21. * 本地上传错误信息
  22. * @var string
  23. */
  24. private $error = '';
  25. /**
  26. * 构造函数,设置storage的domain, 如果有传配置,则domain为配置项,如果没有传domain为第一个路径的目录名称。
  27. * @param mixed $config 上传配置
  28. */
  29. public function __construct($config = null)
  30. {
  31. if (is_array($config) && !empty($config['domain'])) {
  32. $this->domain = strtolower($config['domain']);
  33. }
  34. }
  35. /**
  36. * 检测上传根目录
  37. * @param string $rootpath 根目录
  38. * @return boolean true-检测通过,false-检测失败
  39. */
  40. public function checkRootPath($rootpath)
  41. {
  42. $rootpath = trim($rootpath, './');
  43. if (!$this->domain) {
  44. $rootpath = explode('/', $rootpath);
  45. $this->domain = strtolower(array_shift($rootpath));
  46. $rootpath = implode('/', $rootpath);
  47. }
  48. $this->rootPath = $rootpath;
  49. $st = new \SaeStorage();
  50. if (false === $st->getDomainCapacity($this->domain)) {
  51. $this->error = '您好像没有建立Storage的domain[' . $this->domain . ']';
  52. return false;
  53. }
  54. return true;
  55. }
  56. /**
  57. * 检测上传目录
  58. * @param string $savepath 上传目录
  59. * @return boolean 检测结果,true-通过,false-失败
  60. */
  61. public function checkSavePath($savepath)
  62. {
  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. {
  73. $filename = ltrim($this->rootPath . '/' . $file['savepath'] . $file['savename'], '/');
  74. $st = new \SaeStorage();
  75. /* 不覆盖同名文件 */
  76. if (!$replace && $st->fileExists($this->domain, $filename)) {
  77. $this->error = '存在同名文件' . $file['savename'];
  78. return false;
  79. }
  80. /* 移动文件 */
  81. if (!$st->upload($this->domain, $filename, $file['tmp_name'])) {
  82. $this->error = '文件上传保存错误![' . $st->errno() . ']:' . $st->errmsg();
  83. return false;
  84. } else {
  85. $file['url'] = $st->getUrl($this->domain, $filename);
  86. }
  87. return true;
  88. }
  89. public function mkdir()
  90. {
  91. return true;
  92. }
  93. /**
  94. * 获取最后一次上传错误信息
  95. * @return string 错误信息
  96. */
  97. public function getError()
  98. {
  99. return $this->error;
  100. }
  101. }