FormUploader.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace Qiniu\Storage;
  3. use Qiniu\Config;
  4. use Qiniu\Http\Error;
  5. use Qiniu\Http\Client;
  6. use Qiniu\Http\RequestOptions;
  7. final class FormUploader
  8. {
  9. /**
  10. * 上传二进制流到七牛, 内部使用
  11. *
  12. * @param string $upToken 上传凭证
  13. * @param string $key 上传文件名
  14. * @param string $data 上传二进制流
  15. * @param Config $config 上传配置
  16. * @param string $params 自定义变量,规格参考
  17. * {@link https://developer.qiniu.com/kodo/manual/1235/vars#xvar}
  18. * @param string $mime 上传数据的mimeType
  19. * @param string $fname
  20. * @param RequestOptions $reqOpt
  21. *
  22. * @return array 包含已上传文件的信息,类似:
  23. * [
  24. * "hash" => "<Hash string>",
  25. * "key" => "<Key string>"
  26. * ]
  27. */
  28. public static function put(
  29. $upToken,
  30. $key,
  31. $data,
  32. $config,
  33. $params,
  34. $mime,
  35. $fname,
  36. $reqOpt = null
  37. ) {
  38. if ($reqOpt == null) {
  39. $reqOpt = new RequestOptions();
  40. }
  41. $fields = array('token' => $upToken);
  42. if ($key === null) {
  43. } else {
  44. $fields['key'] = $key;
  45. }
  46. //enable crc32 check by default
  47. $fields['crc32'] = \Qiniu\crc32_data($data);
  48. if ($params) {
  49. foreach ($params as $k => $v) {
  50. $fields[$k] = $v;
  51. }
  52. }
  53. list($accessKey, $bucket, $err) = \Qiniu\explodeUpToken($upToken);
  54. if ($err != null) {
  55. return array(null, $err);
  56. }
  57. list($upHost, $err) = $config->getUpHostV2($accessKey, $bucket, $reqOpt);
  58. if ($err != null) {
  59. return array(null, $err);
  60. }
  61. $response = Client::multipartPost(
  62. $upHost,
  63. $fields,
  64. 'file',
  65. $fname,
  66. $data,
  67. $mime,
  68. array(),
  69. $reqOpt
  70. );
  71. if (!$response->ok()) {
  72. return array(null, new Error($upHost, $response));
  73. }
  74. return array($response->json(), null);
  75. }
  76. /**
  77. * 上传文件到七牛,内部使用
  78. *
  79. * @param string $upToken 上传凭证
  80. * @param string $key 上传文件名
  81. * @param string $filePath 上传文件的路径
  82. * @param Config $config 上传配置
  83. * @param string $params 自定义变量,规格参考
  84. * https://developer.qiniu.com/kodo/manual/1235/vars#xvar
  85. * @param string $mime 上传数据的mimeType
  86. *
  87. * @return array 包含已上传文件的信息,类似:
  88. * [
  89. * "hash" => "<Hash string>",
  90. * "key" => "<Key string>"
  91. * ]
  92. */
  93. public static function putFile(
  94. $upToken,
  95. $key,
  96. $filePath,
  97. $config,
  98. $params,
  99. $mime,
  100. $reqOpt = null
  101. ) {
  102. if ($reqOpt == null) {
  103. $reqOpt = new RequestOptions();
  104. }
  105. $fields = array('token' => $upToken, 'file' => self::createFile($filePath, $mime));
  106. if ($key !== null) {
  107. $fields['key'] = $key;
  108. }
  109. $fields['crc32'] = \Qiniu\crc32_file($filePath);
  110. if ($params) {
  111. foreach ($params as $k => $v) {
  112. $fields[$k] = $v;
  113. }
  114. }
  115. $fields['key'] = $key;
  116. $headers = array('Content-Type' => 'multipart/form-data');
  117. list($accessKey, $bucket, $err) = \Qiniu\explodeUpToken($upToken);
  118. if ($err != null) {
  119. return array(null, $err);
  120. }
  121. list($upHost, $err) = $config->getUpHostV2($accessKey, $bucket, $reqOpt);
  122. if ($err != null) {
  123. return array(null, $err);
  124. }
  125. $response = Client::post($upHost, $fields, $headers, $reqOpt);
  126. if (!$response->ok()) {
  127. return array(null, new Error($upHost, $response));
  128. }
  129. return array($response->json(), null);
  130. }
  131. private static function createFile($filename, $mime)
  132. {
  133. // PHP 5.5 introduced a CurlFile object that deprecates the old @filename syntax
  134. // See: https://wiki.php.net/rfc/curl-file-upload
  135. if (function_exists('curl_file_create')) {
  136. return curl_file_create($filename, $mime);
  137. }
  138. // Use the old style if using an older version of PHP
  139. $value = "@{$filename}";
  140. if (!empty($mime)) {
  141. $value .= ';type=' . $mime;
  142. }
  143. return $value;
  144. }
  145. }