FormUploader.php 4.1 KB

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