Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New Coder
    Join Date
    Apr 2006
    Posts
    50
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Saving Image From Browser Shows As PHP File Or Other

    Hello there,
    I have a quick script that I wrote to display some images in a gallery format. I wanted to track the views of each image, so instead of directing linking each thumbnail to the larger image, i linked it to a php file which updated a database field and then redirects to the image using headers.

    PHP Code:
    if($_GET['image']){
        
    $id $_GET['image'];
        
    mysql_query("UPDATE downloads SET downloads= downloads + 1 WHERE id = '$id'"); 
        
    $sql mysql_query("SELECT link FROM downloads WHERE id = '$id'");
        
    $row mysql_fetch_object($sql);
        
    header("Location: " $row->link);

    The problem is, after the image is now opened in the browser window, and you go to save it, it doesn't save as the proper image type. In firefox, it will always save as a PHP file, and IE, always a bitmap (when they are jpg's).

    For example, visit here. Click on an image, and then right click and try to "save image as". You should notice the problem there.

    Any help is appreciated. Thanks!

  • #2
    Regular Coder mlse's Avatar
    Join Date
    Mar 2005
    Posts
    624
    Thanks
    20
    Thanked 19 Times in 18 Posts
    Seems to work fine for me! I'm using the GNOME Web Browser under Debian Etch ...

  • #3
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,042
    Thanks
    19
    Thanked 42 Times in 42 Posts
    works fine for me too, FF2.0 win xp sp2

  • #4
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    It tries to save it as a .bmp for me too: win xp sp 2 IE 7
    I've always had this problem in i.e. tho...I've never researched it tho...
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #5
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,517
    Thanks
    114
    Thanked 110 Times in 109 Posts
    its not working for me. Click one of the actual images so you go through to see it on its own no right click and save as the name says gallery.php
    You can not say you know how to do something, until you can teach it to someone else.

  • #6
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Can you post the code for gallery.php? Be sure to remove your mysql login information.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #7
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    2,958
    Thanks
    2
    Thanked 304 Times in 296 Posts
    This is a problem of setting the proper headers in the "force download" code to specify the file name for the download, the content type, and the disposition... I don't have info off of the top of my head to post, but you can probably search and get it.

    Edit: The following should work for any file, were the $file variable contains the name of the file being downloaded -
    PHP Code:
    header ("Content-type: octet/stream");
    header ("Content-disposition: attachment; filename=".$file.";");
    header("Content-Length: ".filesize($file)); 
    Edit2: After re-reading the first post, the above headers I posted are for a link that "forces a download", not for the case where you have displayed an image and you right click on it to save it. I believe that if you change from using a redirect to the image - header("Location: "... in that code to reading and outputing a content type and the image data, that your problem can be solved.
    Last edited by CFMaBiSmAd; 07-31-2007 at 03:05 PM.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #8
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    2,958
    Thanks
    2
    Thanked 304 Times in 296 Posts
    I played with your code a little. Instead of redirecting to the image, just display the image on the page. Change the following -
    PHP Code:
    header("Location: " $row->link); 
    to something like this (untested in that I did not have your db to use with this exact syntax, but worked by putting the resultant file name of an image I got from your site) -
    PHP Code:
    echo "<img src='{$row->link}' alt=''>"
    Last edited by CFMaBiSmAd; 07-31-2007 at 03:45 PM. Reason: Clearified what untested/worked meant
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #9
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    works fine for me , ubuntu + firefox
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #10
    New Coder
    Join Date
    Apr 2006
    Posts
    50
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I solved this by just embedding the image into a page via HTML.

  • #11
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,042
    Thanks
    19
    Thanked 42 Times in 42 Posts
    well when i clicked an image to see the image on its own it, it tried saving
    starcraft-ii-20070711085602126.jpg
    so i dunno whats with my comp :S:S

  • #12
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,517
    Thanks
    114
    Thanked 110 Times in 109 Posts
    lol this all sounds a bit strange to me, can someone explain?
    You can not say you know how to do something, until you can teach it to someone else.

  • #13
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Quote Originally Posted by timgolding View Post
    lol this all sounds a bit strange to me, can someone explain?
    Explain what?

    When you clicked on the image you could see it in the browser but when you went to save image as it would try to save it as gallery.php rather than the actual image. The OP wanted the image to save as filename.jpg so they just embed it into an html page when the image is clicked on which seems to allow the image to be saved properly.
    ||||If you are getting paid to do a job, don't ask for help on it!||||


  •  

    Posting Permissions

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