简体   繁体   中英

force file download via amazon s3 php class

My application is using tpyo/amazon-s3-php-class to put and get objects to/from Amazon S3 storage.

Files are not available publicly and I want my users to be able to download private files through my application.

amazon-s3-php-class offers two options to get the file:

  1. Save an object to file: S3::getObject($bucketName, $uploadName, $saveName)
  2. Save an object to a resource of any type: S3::getObject($bucketName, $uploadName, fopen('savefile.txt', 'wb'))

But both methods save a file to my web server. How can I force file download to a client's computer?

I believe you could use the second option, passing the output buffer as the filename argument for fopen (after setting the correct headers):

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
//etc etc

S3::getObject($bucketName, $uploadName, fopen('php://output', 'wb'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM