Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The php scripts that have been mentioned so far will work ok, but the main downside is that every time a visitor on your site requests a file, your own servers will load it from the S3 and then relay that data to the browser. For low traffic sites, it's probably not a big deal, but for high traffic ones, you definitely want to avoid running everything through your own servers.</p> <p>Luckily, there's a fairly straight-forward way to set your files to be forced to download from the S3. And you're exactly right - you just want to set the content-type and content-disposition (just setting content-disposition will work in some browsers, but setting both should work in all browsers).</p> <p>This code is assuming that you're using the Amazon S3 PHP class from Undesigned:</p> <pre><code>&lt;?php // set S3 auth and get your bucket listing // then loop through the bucket and copy each file over itself, replacing the "request headers": S3::copyObject($bucketName, $filename, $bucketName, $filename, "public-read", array(), array("Content-Type" =&gt; "application/octet-stream", "Content-Disposition" =&gt; "attachment")); ?&gt; </code></pre> <p>Now all your files will be forced to download. You may need to clear your cache to see the change. And obviously, don't do that on any file that you actually do want to be loaded "inline" in the browser.</p> <p>The nice part with this solution is that applications that load media files directly (like let's say an mp3 player in Flash) don't care about the content-type or content-disposition, so you can still play your files in the browser and then link to download that same file. If the user already finished loading the file in flash, they'll most likely still have it in their cache, which means their download will be super quick and it won't even cost you any extra bandwidth charges from the S3.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload