Lite.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /*
  3. * +----------------------------------------------------------------------
  4. * | 图片服务器上传接口
  5. * +----------------------------------------------------------------------
  6. * | Copyright (c) 2015 summer All rights reserved.
  7. * +----------------------------------------------------------------------
  8. * | Author: summer <aer_c@qq.com> <qq7579476>
  9. * +----------------------------------------------------------------------
  10. * | This is not a free software, unauthorized no use and dissemination.
  11. * +----------------------------------------------------------------------
  12. * | Date
  13. * +----------------------------------------------------------------------
  14. */
  15. class UCloud_Lite {
  16. protected $config = array(
  17. //上传的API地址,不带http://
  18. 'api' => '',
  19. //统一的key
  20. 'accessKey' => '',
  21. 'secretKey' => '',
  22. //自定义配置的空间
  23. 'bucket' => '',
  24. 'host' => '', //必带http://
  25. 'timeout' => 60
  26. );
  27. //上传文件信息
  28. private $upload_file;
  29. //文件存储的默认路径
  30. private $default_path = '';
  31. //文件存储路径
  32. private $save_path;
  33. //上传文件名
  34. public $file_name;
  35. //上传文件后缀名
  36. private $ext;
  37. //错误信息
  38. public $error = '';
  39. public function __construct() {
  40. if(DI()->config->get('app.UCloud'))
  41. $this->config = array_merge($this->config, DI()->config->get('app.UCloud'));
  42. }
  43. /**
  44. * 设置
  45. *
  46. * @param mixed $key
  47. * @param mixed $value
  48. */
  49. public function set($key,$value){
  50. $this->$key = $value;
  51. }
  52. /**
  53. * 读取
  54. */
  55. public function get($key){
  56. return $this->$key;
  57. }
  58. /**
  59. * 上传操作
  60. *
  61. * @param string $field 上传的文件信息
  62. * @return array or bool
  63. */
  64. public function upfile($fileData) {
  65. //上传文件
  66. $this->upload_file = $fileData;
  67. if ($this->upload_file['tmp_name'] == ""){
  68. $this->setError('File is not empty!');
  69. return false;
  70. }
  71. //文件后缀名
  72. $tmp_ext = explode(".", $this->upload_file['name']);
  73. $tmp_ext = $tmp_ext[count($tmp_ext) - 1];
  74. $this->ext = strtolower($tmp_ext);
  75. //设置文件名称
  76. //if(empty($this->file_name)){
  77. $this->setFileName();
  78. //}
  79. $config = $this->config;
  80. //获取上传引擎信息
  81. //DI()->loader->addDirs('Library/UCloud');
  82. $engine = 'UCloud_Engine_' . ucfirst(DI()->config->get('app.UCloudEngine'));
  83. $upload = new $engine('',$config);
  84. //设置图片信息
  85. $file = $this->upload_file;
  86. $file['savepath'] = $this->setPath();
  87. $file['savename'] = $this->file_name;
  88. //开始上传
  89. $res = $upload->save($file);
  90. if (!$res) {
  91. DI()->logger->debug('failed to upload file to '. $engine,
  92. array('Err' => $upload->getError()));
  93. return false;
  94. } else {
  95. $fileName = $this->setPath() . '/' . $this->file_name;
  96. $fileUrl = $config['host'] . '/' . $fileName;
  97. DI()->logger->debug('succeed to upload file to '.$engine, $fileUrl);
  98. return array(
  99. 'url' => $fileUrl,
  100. 'file' => $fileName
  101. );
  102. }
  103. }
  104. /**
  105. * 设置文件名称 不包括 文件路径
  106. *
  107. * 生成(从2000-01-01 00:00:00 到现在的秒数+微秒+四位随机)
  108. */
  109. private function setFileName(){
  110. if(empty($this->file_name)){
  111. $tmp_name = sprintf('%010d',time() - 946656000)
  112. . sprintf('%03d', microtime() * 1000)
  113. . sprintf('%04d', mt_rand(0,9999));
  114. }else{
  115. $tmp_name = $this->file_name;
  116. }
  117. $this->file_name = $tmp_name . '.' . $this->ext;
  118. }
  119. /**
  120. * 设置文件存储路径
  121. */
  122. private function setPath(){
  123. if($this->save_path)
  124. return $this->default_path . '/' . $this->save_path;
  125. else
  126. return $this->default_path;
  127. }
  128. /**
  129. * 设置错误信息
  130. *
  131. * @param string $error 错误信息
  132. * @return bool 布尔类型的返回结果
  133. */
  134. private function setError($error){
  135. $this->error = $error;
  136. }
  137. }