Force download for image

Discussion in 'Programming' started by orionoreo, Oct 6, 2009.

  1. #1
    Say I have a link to an image <a href="something.jpg">Save</a> how do I force it to ask user where they want to download instead of opening the image in a browser
     
    orionoreo, Oct 6, 2009 IP
  2. Jj delc

    Jj delc Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    What the browser does is not really on your control (or shouldnt). But what happens whenever a browser follows a link, is check the content typ returned by the HTTP header of the web server. Then the browser has a configuraion for many of these, in Image files the action is usually "display on browser window"(or something similar)

    If you wnt to force the download, set your webserver to serve the file with another content type so the browser won't know it is an image and ask the user what to do with the file. Save it or open it with some other program.
     
    Jj delc, Oct 6, 2009 IP
  3. Haney

    Haney Peon

    Messages:
    91
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    If you have PHP, goto elouai.com/force-download.php and use that.
     
    Haney, Oct 6, 2009 IP
  4. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #4
    Add the following to your htaccess file (or create one):

    
    <Files *.jpg>
    ForceType applicaton/octet-stream
    </Files>
    
    Code (markup):
     
    ThePHPMaster, Oct 6, 2009 IP
  5. orionoreo

    orionoreo Peon

    Messages:
    145
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    hmmm thanks guys... I'm going to try it out... but I'm not necessarily looking to turn all; say, jpg in force download. Just have a save button for those users who doesn't know about right click -> save image as. But maybe its not really necessary
     
    orionoreo, Oct 7, 2009 IP
  6. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #6
    Bohra, Oct 7, 2009 IP
  7. silotka

    silotka Peon

    Messages:
    70
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    use php headers :

    header('Content-Description: File Transfer');
    header('Content-Type: application/force-download');
    header('Content-Length: 134253463);
    header('Content-Disposition: attachment; filename=YOUR-IMAGE.PNG');
     
    silotka, Oct 7, 2009 IP
  8. orionoreo

    orionoreo Peon

    Messages:
    145
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    thank you very much
     
    orionoreo, Oct 7, 2009 IP