简体   繁体   中英

Changing the filename of a file stored on Amazon S3 upon download

So I have an upload service with many people uploading the same files to my Amazon S3 bucket. I changed my app design so the SHA1 of the file is calculated upon upload and checked against the list of uploaded files.

If it exists, I simply assign the file to the new uploader as well.

The problem with this is, the file being named as the first uploader named it. All subsequent uploaders will get the same first name.

I can use download="" attribute in HTML5 but it doesn't work in IE: http://caniuse.com/#search=download

The files are stored remotely so I can't change the header unless I download it first to my local server which is illogical.

Please advice.

The only way I see this possible is to add a meta-data just before redirecting user to download.

You can add meta-data to your files in S3. With key as "Content-Disposition" and value as 'attachment; filename="~actual file name~"'. You can force the name and trigger download.

This way, you don't have to download any files to local file system.

The caveat to this is if someone else is also requesting the same file with in milli-seconds, first user might get the name as requested by second user.

use the rename() method

example:

$file = "boo.png";
$newName = "scary";
$ext = substr( $file, strpos( "." ), strlen( $file ) );
$path = "/images/downloaded/";

rename($path . $file, $path . $newName . $ext);

php documentation

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