I have seen this method being used on about three sites now, including Facebook, Dropbox and Microsoft's Skydrive. It works like this. Let's say you want to look at the image without downloading, then you'd just do this.
https://fbcdn-sphotos-a.akamaihd.net/hphotos-ak-xxxx/xxx_xxxxxxxxxxxxxxx_xxxxxxxxx_o.jpg
But if I want to download it, I'd add ?dl=1
https://fbcdn-sphotos-a.akamaihd.net/hphotos-ak-xxxx/xxx_xxxxxxxxxxxxxxx_xxxxxxxxx_o.jpg?dl=1
Easy peasy right? Well, it's probably not easy on the server side, and this is where my problem is. I would know how to do this if that .jpg-file was a PHP script and the $_GET parameter pointed to the image and another parameter would specify whether the image were to be downloaded or not. But that's not the case.
So, what methods did I try? None. Because I honestly have no idea how this works, it's like magic to me. Maybe it's something that you do in .htaccess? That sounds reasonable to me, but after a while of googling I didn't find anything even close to what I'm asking for.
You have some options.
One option would be to use a PHP script instead of the .jpg file. So your URL would point to a PHP file and in the PHP file you would do something like this:
Another option would be to use
mod_rewrite
in your .htaccess file to check for?dl=1
and if found, redirect to the PHP script that will download the file (the same way as above).I'm sure there are more options, but those two are the only ones popping into my head right now.
The display response headers on such a facebook URL:
And the download response headers:
See the
Content-Disposition: attachment
line which is a difference.So as you're already serving the images from a PHP script, in case the download parameter is set, add:
and you should be fine.
I would have redirected all of the images to a single PHP file that will handle them based on their URI parameters.
in the .htaccess I would put:
that will make all of the requests to files with the extensions of jpg, png and gif redirect to you images.php file.
in the images.php file I would search for the existence of ?dl=1 and by that decide how to serve the image: