Tietuku.class.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. class Tietuku
  13. {
  14. /**
  15. * 上传文件根目录
  16. * @var string
  17. */
  18. private $rootPath;
  19. /**
  20. * 上传错误信息
  21. * @var string
  22. */
  23. private $error = '';
  24. private $config = array(
  25. 'secretKey' => '', //贴图库sk
  26. 'accessKey' => '', //贴图库ak
  27. 'aid' => '', //贴图库相册id
  28. );
  29. /**
  30. * 构造函数,用于设置上传根路径
  31. * @param array $config FTP配置
  32. */
  33. public function __construct($config)
  34. {
  35. $this->config = array_merge($this->config, $config);
  36. require_once __DIR__ . DIRECTORY_SEPARATOR . 'Tietuku' . DIRECTORY_SEPARATOR . 'tietuku.class.php';
  37. /* 设置根目录 */
  38. $this->Client = new \TTKClient($this->config['accessKey'], $this->config['secretKey']);
  39. }
  40. /**
  41. * 检测上传根目录(贴图库上传时支持自动创建目录,直接返回)
  42. * @param string $rootpath 根目录
  43. * @return boolean true-检测通过,false-检测失败
  44. */
  45. public function checkRootPath($rootpath)
  46. {
  47. $this->rootPath = trim($rootpath, './') . '/';
  48. return true;
  49. }
  50. /**
  51. * 检测上传目录(贴图库上传时支持自动创建目录,直接返回)
  52. * @param string $savepath 上传目录
  53. * @return boolean 检测结果,true-通过,false-失败
  54. */
  55. public function checkSavePath($savepath)
  56. {
  57. return true;
  58. }
  59. /**
  60. * 创建文件夹 (贴图库上传时支持自动创建目录,直接返回)
  61. * @param string $savepath 目录名称
  62. * @return boolean true-创建成功,false-创建失败
  63. */
  64. public function mkdir($savepath)
  65. {
  66. return true;
  67. }
  68. /**
  69. * 保存指定文件
  70. * @param array $file 保存的文件信息
  71. * @param boolean $replace 同名文件是否覆盖
  72. * @return boolean 保存状态,true-成功,false-失败
  73. */
  74. public function save(&$file, $replace = true)
  75. {
  76. $file['name'] = $file['savepath'] . $file['savename'];
  77. $result = $this->Client->uploadFile($this->config['aid'], $file['tmp_name'], $file['name']);
  78. $result = json_decode($result, true);
  79. d_f('upload', $result);
  80. if (isset($result[0])) {
  81. $result = $result[0];
  82. }
  83. if (isset($result['code'])) {
  84. $this->error = 'token错误';
  85. } else {
  86. $file['url'] = $result['linkurl'];
  87. }
  88. return isset($result['width']) ? true : false;
  89. }
  90. /**
  91. * 获取最后一次上传错误信息
  92. * @return string 错误信息
  93. */
  94. public function getError()
  95. {
  96. return $this->error;
  97. }
  98. }