how to prompt for download when click on image?

Discussion in 'PHP' started by bobocheez, Jan 25, 2010.

  1. #1
    Hi
    How can I get a user to download an image when the image is clicked?
     
    bobocheez, Jan 25, 2010 IP
  2. Haney

    Haney Peon

    Messages:
    91
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Haney, Jan 25, 2010 IP
  3. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #3
    danx10, Jan 25, 2010 IP
  4. bobocheez

    bobocheez Active Member

    Messages:
    403
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    55
    #4
    Those are both great scripts, but is it possible to make it so that the file is on another server?

    For instance; User clicks on a download link on site A, but the image is on site B.

    Note: I did set the filepaths to the location of the image, but no results.
     
    bobocheez, Jan 26, 2010 IP
  5. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #5
    The reason that didn't work was because, file_exists only works on locally hosted files and not remote (too my knowledge). Theifore use this...

    <?php
    $file = 'http://forums.digitalpoint.com/images/misc/dps_logo.gif';
    
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($file));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        readfile($file);
        exit;
    ?>
    PHP:
     
    Last edited: Jan 26, 2010
    danx10, Jan 26, 2010 IP
  6. bobocheez

    bobocheez Active Member

    Messages:
    403
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    55
    #6
    Is the content type not already defined?

    case "gif": $ctype="image/gif"; break;
      case "png": $ctype="image/png"; break;
      case "jpeg":
      case "jpg": $ctype="image/jpg"; break;
    PHP:
     
    bobocheez, Jan 26, 2010 IP
  7. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #7
    Yes, but you didn't mention that earlier.

    Follow this example -> http://forums.digitalpoint.com/showpost.php?p=13461746&postcount=5
     
    danx10, Jan 26, 2010 IP
  8. bobocheez

    bobocheez Active Member

    Messages:
    403
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    55
    #8
    Oh, your the one who showed me that script lol.

    But the example seems to be working, except you cannot actually see the image. So when the user clicks download, it seems like its downloading the image, but when you click on it, it says no preview available. Image editors don't seem to be able to interpret anything either.

    Any suggestions?
     
    bobocheez, Jan 26, 2010 IP
  9. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #9
    This should work :p

    <?php
    
    $file ="http://forums.digitalpoint.com/images/misc/dps_logo.gif";
    
    switch($ext)
    {
      case "gif": $ctype="image/gif"; break;
      case "png": $ctype="image/png"; break;
      case "jpeg":
      case "jpg": $ctype="image/jpg"; break;
      default: $ctype="application/force-download";
    }
    
    header("Content-Type: $ctype");
    header("Content-Disposition: attachment; filename=".basename($file)); 
    header("Content-Transfer-Encoding: binary");
    
    readfile($file);
    
    ?>
    PHP:
     
    danx10, Jan 26, 2010 IP
  10. bobocheez

    bobocheez Active Member

    Messages:
    403
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    55
    #10
    Very nice. Wroks like a charm.
    Thank you very much!
     
    bobocheez, Jan 26, 2010 IP
  11. Haney

    Haney Peon

    Messages:
    91
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    That should work for image files saved in a mysql database right?
     
    Haney, Jan 26, 2010 IP
  12. bobocheez

    bobocheez Active Member

    Messages:
    403
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    55
    #12
    Technically, you cannot store files in the database. But if by file, you mean link, then yes.

    But remember that you have to connect the user to your database. So, if you can see the file with a URL, then you can force a download with the scripts provided above.
     
    bobocheez, Jan 26, 2010 IP