upload_and_callback.php 995 B

12345678910111213141516171819202122232425262728293031
  1. <?php
  2. require_once __DIR__ . '/../autoload.php';
  3. use Qiniu\Auth;
  4. use Qiniu\Storage\UploadManager;
  5. // 控制台获取密钥:https://portal.qiniu.com/user/key
  6. $accessKey = getenv('QINIU_ACCESS_KEY');
  7. $secretKey = getenv('QINIU_SECRET_KEY');
  8. $bucket = getenv('QINIU_TEST_BUCKET');
  9. $auth = new Auth($accessKey, $secretKey);
  10. // 上传完成后通知到你的业务服务器(需要可以公网访问,并能够相应 200 OK)
  11. // 上传策略参数:https://developer.qiniu.com/kodo/manual/1206/put-policy
  12. $policy = array(
  13. 'callbackUrl' => 'http://your.domain.com/upload_verify_callback.php',
  14. 'callbackBody' => 'filename=$(fname)&filesize=$(fsize)'
  15. );
  16. $uptoken = $auth->uploadToken($bucket, null, 3600, $policy);
  17. // 上传文件的本地路径
  18. $filePath = './php-logo.png';
  19. $uploadMgr = new UploadManager();
  20. list($ret, $err) = $uploadMgr->putFile($uptoken, null, $filePath);
  21. echo "\n====> putFile result: \n";
  22. if ($err !== null) {
  23. var_dump($err);
  24. } else {
  25. var_dump($ret);
  26. }