io.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. require_once("http.php");
  3. require_once("auth_digest.php");
  4. // ----------------------------------------------------------
  5. // class Qiniu_PutExtra
  6. class Qiniu_PutExtra
  7. {
  8. public $Params = null;
  9. public $MimeType = null;
  10. public $Crc32 = 0;
  11. public $CheckCrc = 0;
  12. }
  13. function Qiniu_Put($upToken, $key, $body, $putExtra) // => ($putRet, $err)
  14. {
  15. global $QINIU_UP_HOST;
  16. if ($putExtra === null) {
  17. $putExtra = new Qiniu_PutExtra;
  18. }
  19. $fields = array('token' => $upToken);
  20. if ($key === null) {
  21. $fname = '?';
  22. } else {
  23. $fname = $key;
  24. $fields['key'] = $key;
  25. }
  26. if ($putExtra->CheckCrc) {
  27. $fields['crc32'] = $putExtra->Crc32;
  28. }
  29. $files = array(array('file', $fname, $body));
  30. $client = new Qiniu_HttpClient;
  31. return Qiniu_Client_CallWithMultipartForm($client, $QINIU_UP_HOST, $fields, $files);
  32. }
  33. function Qiniu_PutFile($upToken, $key, $localFile, $putExtra) // => ($putRet, $err)
  34. {
  35. global $QINIU_UP_HOST;
  36. if ($putExtra === null) {
  37. $putExtra = new Qiniu_PutExtra;
  38. }
  39. $fields = array('token' => $upToken, 'file' => '@' . $localFile);
  40. if ($key === null) {
  41. $fname = '?';
  42. } else {
  43. $fname = $key;
  44. $fields['key'] = $key;
  45. }
  46. if ($putExtra->CheckCrc) {
  47. if ($putExtra->CheckCrc === 1) {
  48. $hash = hash_file('crc32b', $localFile);
  49. $array = unpack('N', pack('H*', $hash));
  50. $putExtra->Crc32 = $array[1];
  51. }
  52. $fields['crc32'] = sprintf('%u', $putExtra->Crc32);
  53. }
  54. $client = new Qiniu_HttpClient;
  55. return Qiniu_Client_CallWithForm($client, $QINIU_UP_HOST, $fields, 'multipart/form-data');
  56. }
  57. // ----------------------------------------------------------