Qiniu.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  1. <?php
  2. namespace app\common\extend\upload;
  3. use Qiniu\Auth;
  4. use Qiniu\Storage\UploadManager;
  5. class Qiniu
  6. {
  7. public $name = '七牛云存储';
  8. public $ver = '1.0';
  9. private $config = [];
  10. public function __construct($config = []) {
  11. $this->config = $config;
  12. }
  13. public function submit($file_path)
  14. {
  15. $bucket = $GLOBALS['config']['upload']['api']['qiniu']['bucket'];
  16. $accessKey = $GLOBALS['config']['upload']['api']['qiniu']['accesskey'];
  17. $secretKey = $GLOBALS['config']['upload']['api']['qiniu']['secretkey'];
  18. require_once ROOT_PATH . 'extend/qiniu/autoload.php';
  19. $auth = new Auth($accessKey, $secretKey);
  20. $return = '{"newName":"$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)","oldName":"$(fname)","width":"$(imageInfo.width)","height":"$(imageInfo.height)"}';
  21. $return = array('returnBody' => $return);
  22. $expires = 3600;
  23. $token = $auth->uploadToken($bucket,$file_path,$expires,$return);
  24. $filePath = ROOT_PATH . $file_path;
  25. $uploadMgr = new UploadManager();
  26. $a = $uploadMgr->putFile($token, $file_path, $filePath);
  27. empty($this->config['keep_local']) && @unlink($filePath);
  28. return $GLOBALS['config']['upload']['api']['qiniu']['url'] . '/' . $file_path;
  29. }
  30. }