upload_and_callback.php 934B

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