Hi, how to force a link to download image files, instead of open it in the browser. Thanks.
Printable View
Hi, how to force a link to download image files, instead of open it in the browser. Thanks.
You can google this, "force download". But it makes use of tricking the browser and it works most of the time, but it's not really a proper technique. You also need to change the headers on the server or through a serverside language, so you can't do this easily, such as with the image file itself.
Here is a link to an example script. It's fun to use, but I have not had any need for it yet.
link
I tested it out just now in the Opera browser and it worked just fine.
Thanks @Daniel find the solution using that keyword.
Now i use this code on htaccess:
and it work.Code:AddType application/octet-stream .jpg
thanks @james438 for that code. But all of my code are pure HTML.
I was not aware of that little trick. I want to add that I discovered that it does not work if the image is already displayed on the same page with the link to the image.
@James,
I try your code, and i'm a little bit confuse with all of that PHP thing.
But i try to take the main part, here:
and to download the image i use this link:Code:<?php
// Define the image type. We can remove this but not recommended
header("Content-type: image/jpeg");
// Define the name of image after downloaded
header('Content-Disposition: attachment; filename="bg_about1.jpg"');
// Read the original image file
readfile('bg_about1.jpg');
?>
Code:<a href="download.php">download</a>
it work, but the other problem. each download.php (code above) can carry only 1 image.
So there's another problem if i have multiple image to download.
The .htaccess method seems like a good one. I didn't think of that. Technically it's a serverside language, but it's a lot easier to deal with than PHP in this case when serving images.
That's solvable, actually. Just add a dummy query string to one or the other:Quote:
Originally Posted by James
..../images/myimage.jpg?randomtext
Often, md5(microtime()) can be used in PHP to achieve this. Or just a random number.
Or, in fact, even just the '?' will work, because it's technically a different URL than the one used on the page.
(The only disadvantage here is that it would require downloading the same image twice-- twice the bandwidth, twice the waiting, but that's probably not a major concern.