View Full Version : Resolved Force link to download image not open
davelf
05-30-2012, 06:23 AM
Hi, how to force a link to download image files, instead of open it in the browser. Thanks.
djr33
05-30-2012, 06:35 AM
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.
james438
05-30-2012, 07:15 AM
Here is a link to an example script. It's fun to use, but I have not had any need for it yet.
link (http://www.dynamicdrive.com/forums/showthread.php?t=63292&p=256149)
I tested it out just now in the Opera browser and it worked just fine.
davelf
05-30-2012, 09:49 AM
Thanks @Daniel find the solution using that keyword.
Now i use this code on htaccess:
AddType application/octet-stream .jpg
and it work.
thanks @james438 for that code. But all of my code are pure HTML.
james438
05-30-2012, 06:06 PM
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.
davelf
05-31-2012, 02:49 AM
@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:
<?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');
?>
and to download the image i use this link:
<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.
djr33
05-31-2012, 03:17 AM
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.
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.That's solvable, actually. Just add a dummy query string to one or the other:
..../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.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.