FormUploader.php 4.1 KB

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