Upyun.class.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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: 小梦科技资源 <zuojiazi@vip.qq.com> <https://www.nanodreamtech.com>
  10. // +----------------------------------------------------------------------
  11. namespace Think\Upload\Driver;
  12. class Upyun{
  13. /**
  14. * 上传文件根目录
  15. * @var string
  16. */
  17. private $rootPath;
  18. /**
  19. * 上传错误信息
  20. * @var string
  21. */
  22. private $error = '';
  23. private $config = array(
  24. 'host' => '', //又拍云服务器
  25. 'username' => '', //又拍云用户
  26. 'password' => '', //又拍云密码
  27. 'bucket' => '', //空间名称
  28. 'timeout' => 90, //超时时间
  29. );
  30. /**
  31. * 构造函数,用于设置上传根路径
  32. * @param array $config FTP配置
  33. */
  34. public function __construct($config){
  35. /* 默认FTP配置 */
  36. $this->config = array_merge($this->config, $config);
  37. $this->config['password'] = md5($this->config['password']);
  38. }
  39. /**
  40. * 检测上传根目录(又拍云上传时支持自动创建目录,直接返回)
  41. * @param string $rootpath 根目录
  42. * @return boolean true-检测通过,false-检测失败
  43. */
  44. public function checkRootPath($rootpath){
  45. /* 设置根目录 */
  46. $this->rootPath = trim($rootpath, './') . '/';
  47. return true;
  48. }
  49. /**
  50. * 检测上传目录(又拍云上传时支持自动创建目录,直接返回)
  51. * @param string $savepath 上传目录
  52. * @return boolean 检测结果,true-通过,false-失败
  53. */
  54. public function checkSavePath($savepath){
  55. return true;
  56. }
  57. /**
  58. * 创建文件夹 (又拍云上传时支持自动创建目录,直接返回)
  59. * @param string $savepath 目录名称
  60. * @return boolean true-创建成功,false-创建失败
  61. */
  62. public function mkdir($savepath){
  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. $header['Content-Type'] = $file['type'];
  73. $header['Content-MD5'] = $file['md5'];
  74. $header['Mkdir'] = 'true';
  75. $resource = fopen($file['tmp_name'], 'r');
  76. $save = $this->rootPath . $file['savepath'] . $file['savename'];
  77. $data = $this->request($save, 'PUT', $header, $resource);
  78. return false === $data ? false : true;
  79. }
  80. /**
  81. * 获取最后一次上传错误信息
  82. * @return string 错误信息
  83. */
  84. public function getError(){
  85. return $this->error;
  86. }
  87. /**
  88. * 请求又拍云服务器
  89. * @param string $path 请求的PATH
  90. * @param string $method 请求方法
  91. * @param array $headers 请求header
  92. * @param resource $body 上传文件资源
  93. * @return boolean
  94. */
  95. private function request($path, $method, $headers = null, $body = null){
  96. $uri = "/{$this->config['bucket']}/{$path}";
  97. $ch = curl_init($this->config['host'] . $uri);
  98. $_headers = array('Expect:');
  99. if (!is_null($headers) && is_array($headers)){
  100. foreach($headers as $k => $v) {
  101. array_push($_headers, "{$k}: {$v}");
  102. }
  103. }
  104. $length = 0;
  105. $date = gmdate('D, d M Y H:i:s \G\M\T');
  106. if (!is_null($body)) {
  107. if(is_resource($body)){
  108. fseek($body, 0, SEEK_END);
  109. $length = ftell($body);
  110. fseek($body, 0);
  111. array_push($_headers, "Content-Length: {$length}");
  112. curl_setopt($ch, CURLOPT_INFILE, $body);
  113. curl_setopt($ch, CURLOPT_INFILESIZE, $length);
  114. } else {
  115. $length = @strlen($body);
  116. array_push($_headers, "Content-Length: {$length}");
  117. curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
  118. }
  119. } else {
  120. array_push($_headers, "Content-Length: {$length}");
  121. }
  122. array_push($_headers, 'Authorization: ' . $this->sign($method, $uri, $date, $length));
  123. array_push($_headers, "Date: {$date}");
  124. curl_setopt($ch, CURLOPT_HTTPHEADER, $_headers);
  125. curl_setopt($ch, CURLOPT_TIMEOUT, $this->config['timeout']);
  126. curl_setopt($ch, CURLOPT_HEADER, 1);
  127. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  128. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
  129. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
  130. if ($method == 'PUT' || $method == 'POST') {
  131. curl_setopt($ch, CURLOPT_POST, 1);
  132. } else {
  133. curl_setopt($ch, CURLOPT_POST, 0);
  134. }
  135. if ($method == 'HEAD') {
  136. curl_setopt($ch, CURLOPT_NOBODY, true);
  137. }
  138. $response = curl_exec($ch);
  139. $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  140. curl_close($ch);
  141. list($header, $body) = explode("\r\n\r\n", $response, 2);
  142. if ($status == 200) {
  143. if ($method == 'GET') {
  144. return $body;
  145. } else {
  146. $data = $this->response($header);
  147. return count($data) > 0 ? $data : true;
  148. }
  149. } else {
  150. $this->error($header);
  151. return false;
  152. }
  153. }
  154. /**
  155. * 获取响应数据
  156. * @param string $text 响应头字符串
  157. * @return array 响应数据列表
  158. */
  159. private function response($text){
  160. $headers = explode("\r\n", $text);
  161. $items = array();
  162. foreach($headers as $header) {
  163. $header = trim($header);
  164. if(strpos($header, 'x-upyun') !== False){
  165. list($k, $v) = explode(':', $header);
  166. $items[trim($k)] = in_array(substr($k,8,5), array('width','heigh','frame')) ? intval($v) : trim($v);
  167. }
  168. }
  169. return $items;
  170. }
  171. /**
  172. * 生成请求签名
  173. * @param string $method 请求方法
  174. * @param string $uri 请求URI
  175. * @param string $date 请求时间
  176. * @param integer $length 请求内容大小
  177. * @return string 请求签名
  178. */
  179. private function sign($method, $uri, $date, $length){
  180. $sign = "{$method}&{$uri}&{$date}&{$length}&{$this->config['password']}";
  181. return 'UpYun ' . $this->config['username'] . ':' . md5($sign);
  182. }
  183. /**
  184. * 获取请求错误信息
  185. * @param string $header 请求返回头信息
  186. */
  187. private function error($header) {
  188. list($status, $stash) = explode("\r\n", $header, 2);
  189. list($v, $code, $message) = explode(" ", $status, 3);
  190. $message = is_null($message) ? 'File Not Found' : "[{$status}]:{$message}";
  191. $this->error = $message;
  192. }
  193. }