View Full Version : Saving Image From Browser Shows As PHP File Or Other

07-31-2007, 06:28 AM
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.

$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 (http://starcraftzone.com/gallery/). 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!

07-31-2007, 12:33 PM
Seems to work fine for me! I'm using the GNOME Web Browser under Debian Etch ...

07-31-2007, 12:41 PM
works fine for me too, FF2.0 win xp sp2

07-31-2007, 01:28 PM
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...

07-31-2007, 01:29 PM
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

07-31-2007, 01:32 PM
Can you post the code for gallery.php? Be sure to remove your mysql login information.

07-31-2007, 03:28 PM
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 -

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.

07-31-2007, 04:17 PM
I played with your code a little. Instead of redirecting to the image, just display the image on the page. Change the following -

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) -

echo "<img src='{$row->link}' alt=''>";

07-31-2007, 04:23 PM
works fine for me , ubuntu + firefox

07-31-2007, 08:11 PM
I solved this by just embedding the image into a page via HTML.

07-31-2007, 10:56 PM
well when i clicked an image to see the image on its own it, it tried saving
so i dunno whats with my comp :S:S

08-01-2007, 12:44 AM
lol this all sounds a bit strange to me, can someone explain?

08-01-2007, 12:52 AM
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.