views:

2076

answers:

8

I am trying to use http://code.google.com/p/amazon-s3-php-class/ to force-dowload files from AWS S3. I have an mp3 that I want people to "play" or "download." By default the when you access the file directly on s3 it begins to play in the browser. I need to add an option to actually download. I have Googled and found came up with nothing. I conceptually know what needs to happen but don't know how to produce it php. I know I need to modify the headers to Content-Disposition: attachment. Any help would be greatly appreciated.

Thanks, Michael

A: 

php would just download the file to the server, not the client. remember, php doesn't do anything on the client, it just returns content (html, javascript, css, xml, whatever...)

[edit: added for clarity]: php can serve audio content, but you want to serve a web page and audio at the same time. To get that client behaviour, you have to get the client to request the file based on the web page's html or javascript.

So you have to get the client to download the file. For instance, have an iframe on the page with the url of the file on s3. Use css to make the iframe invisible. It should render the page and download and play the mp3.

Otherwise, look into using javascript to kick of a download when the page loads. I'm not sure if that's possible.

Tim Hoolihan
Yes, your right. What I'm looking for as the end result is to have a file i.e. s3download.php that I can pass the file to that needs to be downloaded. So for example the download would look like this <a href="s3download.php?file=test.mp3">Download</a>When the file is called it access' the s3 account, adds the content-disposition headers causing the file to start downloading. Does this make sense?
michaelespinosa
for s3download.php, your steps would be something like:-setup header values for content type to be mp3 data-download from s3 using using a curl, http-request or whatever your preferred method is, writing to a temp file-read the file contents and return to the userI can't put all the samples into a comment, but if you google samples of each of those steps, you should have what you need.
Tim Hoolihan
+1  A: 

Try setting the "Content-Disposition: attachment;" header. This has worked for me in the past.


<?php

header('Content-Type: audio/mpeg');
header('Content-Disposition: attachment; filename=theSong.mp3;');

readfile('some url/theSong.mp3');

exit();

?>
Kevin
A: 

I never tried Amazon's S3 hosting, but don't you have access to using .htaccess files there? Then you can set Content-Type and Content-Disposition for an entire directory with this entry:

<IfModule mod_headers.c>
    <FilesMatch "\.(mp3)$">
            ForceType audio/mpeg
            Header set Content-Disposition attachment
    </FilesMatch>
</IfModule>
Preben
A: 

So modify my example above to be like this


<?php

header('Content-Type: audio/mpeg');
header("Content-Disposition: attachment; filename={$_GET['file']};");

readfile("url to the file/{$_GET['file']}");

exit();

?>

Now you will want to put some validation in there so that you not giving the world access to every file you put on S3, but this should work.

Kevin
+1  A: 

If you are using a library like Tarzan AWS, you can add meta headers, that amazon will include when the file is retrieved. Check out the meta parameter in the update_object function here, for example: http://tarzan-aws.com/docs/2.0/files/s3-class-php.html#AmazonS3.update_object

Corey Maass
+2  A: 
Ben
that's a pretty big downside. I don't think I would consider this question answered.
superjoe30
A: 

Also worth mentioning is you are able to hard-set the headers for files in S3. For example, if you need to force-download a certain file you can set the appropriate headers for that file. Such as defaulting to stream, and either having a secondary file set to force-download or spend the bandwidth to use php/fputs and force-download via PHP.

Mahdi.Montgomery