Lite.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * 七牛接口调用
  4. * 1、图片文件上传
  5. *
  6. * 参考:http://developer.qiniu.com/docs/v6/sdk/php-sdk.html
  7. *
  8. * @author: dogstar 2015-03-17
  9. */
  10. require_once dirname(__FILE__) . '/qiniu/io.php';
  11. require_once dirname(__FILE__) . '/qiniu/rs.php';
  12. class Qiniu_Lite {
  13. protected $config;
  14. /**
  15. * @param string $config['accessKey'] 统一的key
  16. * @param string $config['secretKey']
  17. * @param string $config['space_bucket'] 自定义配置的空间
  18. * @param string $config['space_host']
  19. */
  20. public function __construct($config = NULL) {
  21. $this->config = $config;
  22. if ($this->config === NULL) {
  23. $this->config = DI()->config->get('app.Qiniu');
  24. }
  25. }
  26. /**
  27. * 文件上传
  28. * @param string $filePath 待上传文件的绝对路径
  29. * @return string 上传成功后的URL,失败时返回空
  30. */
  31. /*public function uploadFile($filePath){
  32. $fileUrl = '';
  33. if (!file_exists($filePath)) {
  34. return $fileUrl;
  35. }
  36. $config = $this->config;
  37. $fileName = date('YmdHis_', $_SERVER['REQUEST_TIME'])
  38. . md5(PhalApi_Tool::createRandStr(8) . microtime(true));
  39. Qiniu_SetKeys($config['accessKey'], $config['secretKey']);
  40. $putPolicy = new Qiniu_RS_PutPolicy($config['space_bucket']);
  41. $upToken = $putPolicy->Token(null);
  42. $putExtra = new Qiniu_PutExtra();
  43. $putExtra->Crc32 = 1;
  44. list($ret, $err) = Qiniu_PutFile($upToken, $fileName, $filePath, $putExtra);
  45. if ($err !== null) {
  46. DI()->logger->debug('failed to upload file to qiniu',
  47. array('Err' => $err->Err, 'Reqid' => $err->Reqid, 'Details' => $err->Details, 'Code' => $err->Code));
  48. } else {
  49. $fileUrl = $config['space_host'] . '/' . $fileName;
  50. DI()->logger->debug('succeed to upload file to qiniu', $ret);
  51. }
  52. return $fileUrl;
  53. }*/
  54. /**
  55. * 文件上传(重写)
  56. * @param string $filePath 待上传文件的绝对路径
  57. * @return string 上传成功后的URL,失败时返回空
  58. */
  59. public function uploadFile($filePath,$accesskey,$secretkey,$bucket,$spacehost){
  60. $fileUrl = '';
  61. if (!file_exists($filePath)) {
  62. return $fileUrl;
  63. }
  64. $fileName = date('YmdHis_', $_SERVER['REQUEST_TIME'])
  65. . md5(PhalApi_Tool::createRandStr(8) . microtime(true));
  66. Qiniu_SetKeys($accesskey, $secretkey);
  67. $putPolicy = new Qiniu_RS_PutPolicy($bucket);
  68. $upToken = $putPolicy->Token(null);
  69. $putExtra = new Qiniu_PutExtra();
  70. $putExtra->Crc32 = 1;
  71. list($ret, $err) = Qiniu_PutFile($upToken, $fileName, $filePath, $putExtra);
  72. if ($err !== null) {
  73. DI()->logger->debug('failed to upload file to qiniu',
  74. array('Err' => $err->Err, 'Reqid' => $err->Reqid, 'Details' => $err->Details, 'Code' => $err->Code));
  75. } else {
  76. $fileUrl = $spacehost.$fileName;
  77. DI()->logger->debug('succeed to upload file to qiniu', $ret);
  78. }
  79. return $fileUrl;
  80. }
  81. /**
  82. * 获取七牛Token
  83. * @return string 七牛Token
  84. */
  85. public function getQiniuToken(){
  86. $config = $this->config;
  87. Qiniu_SetKeys($config['accessKey'], $config['secretKey']);
  88. $putPolicy = new Qiniu_RS_PutPolicy($config['space_bucket']);
  89. $upToken = $putPolicy->Token(null);
  90. return $upToken;
  91. }
  92. /**
  93. * 获取七牛Token(方法重写)
  94. * @return string 七牛Token
  95. */
  96. public function getQiniuToken1($accesskey,$secretkey,$bucket){
  97. Qiniu_SetKeys($accesskey, $secretkey);
  98. $putPolicy = new Qiniu_RS_PutPolicy($bucket);
  99. $upToken = $putPolicy->Token(null);
  100. return $upToken;
  101. }
  102. }