-1

A site I'm working on hosts an image library we encourage users to download files from.

We're using the AWS plugin by Delicious Brains to host our static content on a separate server from the main site (url routes to static.domain.com rather than www.domain.com) but how would we be able to add a download link on our site which would automatically trigger the browser to download the file direct from the Amazon instance?

I was hopeful that using the HTML5 download attribute as follows would do the trick:

<a download href="http://static.domain.com/wp-content/uploads/image.jpg">Download Image</a>

but unfortunately this is not supported by IE, Edge or Safari. What would be the correct PHP approach for this so the download request is only processed by S3 and not the main web server?

0

You're going to have to choose between having the file served directly from S3 or having it pop up with a download box for the user.

If you serve the file directly from S3, then the user's browser will determine what to do with it. In the case of images, that will most likely be showing the image in the browser.

If you route the requests through your server, then you can control how the user receives the file. You can add headers to the response that tell the browser that they should prompt the user to save the file instead of trying to display it.

See this guide in the PHP documentation on how to force a file download box: http://php.net/manual/en/function.readfile.php

I would recommend not using this approach, and having the files linked directly to S3, to keep the user in control of what they do with a file. Particularly in the age of mobile, if a user is on an iPhone or an iPad, which don't have a concept of a filesystem, the "download" functionality doesn't make as much sense as opening the image in the same window and allowing the user to utilize native controls to save the image to Photos, or email it, or AirDrop, or what have you.

  • I was afraid there wansn't a solution without routing through the main server but I'm not much of a server side dev so I wanted to check first. Thanks a lot for your help, I guess I'll stick with the download attribute and live with the browser support. – Jazibobs Oct 22 '15 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.