Results 1 to 7 of 7

Thread: Force link to download image not open

  1. #1
    Join Date
    Jul 2009
    Location
    Binus University
    Posts
    459
    Thanks
    78
    Thanked 21 Times in 21 Posts

    Default Force link to download image not open

    Hi, how to force a link to download image files, instead of open it in the browser. Thanks.
    Last edited by davelf; 06-01-2012 at 06:21 AM.
    _____________________

    David Demetrius // davejob
    _____________________

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    1,720
    Thanks
    82
    Thanked 90 Times in 88 Posts

    Default

    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.
    To choose the lesser of two evils is still to choose evil. My personal site

  4. The Following User Says Thank You to james438 For This Useful Post:

    davelf (05-31-2012)

  5. #4
    Join Date
    Jul 2009
    Location
    Binus University
    Posts
    459
    Thanks
    78
    Thanked 21 Times in 21 Posts

    Default

    Thanks @Daniel find the solution using that keyword.
    Now i use this code on htaccess:
    Code:
    AddType application/octet-stream .jpg
    and it work.


    thanks @james438 for that code. But all of my code are pure HTML.
    _____________________

    David Demetrius // davejob
    _____________________

  6. The Following User Says Thank You to davelf For This Useful Post:

    james438 (05-30-2012)

  7. #5
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    1,720
    Thanks
    82
    Thanked 90 Times in 88 Posts

    Default

    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.
    To choose the lesser of two evils is still to choose evil. My personal site

  8. #6
    Join Date
    Jul 2009
    Location
    Binus University
    Posts
    459
    Thanks
    78
    Thanked 21 Times in 21 Posts

    Default

    @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:

    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');
    ?>
    and to download the image i use this link:
    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.
    _____________________

    David Demetrius // davejob
    _____________________

  9. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    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.

    Quote Originally Posted by James
    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.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •