upload_and_callback.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. <?php
  2. require_once __DIR__ . '/../autoload.php';
  3. use Qiniu\Auth;
  4. use Qiniu\Storage\UploadManager;
  5. // use Qiniu\Config;
  6. // use Qiniu\Zone;
  7. // 指定zone上传
  8. // $zone = Zone::qvmZonez0(); //华东QVM内网上传指定host
  9. // $config = new Config($zone);
  10. $accessKey = getenv('QINIU_ACCESS_KEY');
  11. $secretKey = getenv('QINIU_SECRET_KEY');
  12. $bucket = getenv('QINIU_TEST_BUCKET');
  13. $auth = new Auth($accessKey, $secretKey);
  14. // 上传文件到七牛后, 七牛将文件名和文件大小回调给业务服务器.
  15. // 可参考文档: http://developer.qiniu.com/docs/v6/api/reference/security/put-policy.html
  16. $policy = array(
  17. 'callbackUrl' => 'http://your.domain.com/upload_verify_callback.php',
  18. 'callbackBody' => 'filename=$(fname)&filesize=$(fsize)'
  19. );
  20. $uptoken = $auth->uploadToken($bucket, null, 3600, $policy);
  21. //上传文件的本地路径
  22. $filePath = './php-logo.png';
  23. //指定 config
  24. // $uploadMgr = new UploadManager($config);
  25. $uploadMgr = new UploadManager();
  26. list($ret, $err) = $uploadMgr->putFile($uptoken, null, $filePath);
  27. echo "\n====> putFile result: \n";
  28. if ($err !== null) {
  29. var_dump($err);
  30. } else {
  31. var_dump($ret);
  32. }