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.
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 -
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.
__________________
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.
Last edited by CFMaBiSmAd; 07-31-2007 at 03:05 PM..
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=''>";
__________________
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.
Last edited by CFMaBiSmAd; 07-31-2007 at 03:45 PM..
Reason: Clearified what untested/worked meant
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)
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!||||