Sae.class.php 3.3 KB

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