Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-31-2007, 05:28 AM   PM User | #1
xxkylexx
New Coder

 
Join Date: Apr 2006
Posts: 50
Thanks: 2
Thanked 0 Times in 0 Posts
xxkylexx is an unknown quantity at this point
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!
xxkylexx is offline   Reply With Quote
Old 07-31-2007, 11:33 AM   PM User | #2
mlse
Regular Coder

 
mlse's Avatar
 
Join Date: Mar 2005
Posts: 624
Thanks: 20
Thanked 19 Times in 18 Posts
mlse is on a distinguished road
Seems to work fine for me! I'm using the GNOME Web Browser under Debian Etch ...
mlse is offline   Reply With Quote
Old 07-31-2007, 11:41 AM   PM User | #3
rafiki
Senior Coder

 
rafiki's Avatar
 
Join Date: Aug 2006
Location: Floating around somewhere...
Posts: 2,034
Thanks: 18
Thanked 42 Times in 42 Posts
rafiki will become famous soon enough
works fine for me too, FF2.0 win xp sp2
__________________
Get Firefox Now
rafiki is offline   Reply With Quote
Old 07-31-2007, 12:28 PM   PM User | #4
StupidRalph
Senior Coder

 
Join Date: Mar 2003
Location: Atlanta
Posts: 1,037
Thanks: 14
Thanked 30 Times in 28 Posts
StupidRalph is on a distinguished road
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.
StupidRalph is offline   Reply With Quote
Old 07-31-2007, 12:29 PM   PM User | #5
timgolding
Senior Coder

 
timgolding's Avatar
 
Join Date: Aug 2006
Location: Southampton
Posts: 1,460
Thanks: 89
Thanked 110 Times in 109 Posts
timgolding is on a distinguished road
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.
timgolding is offline   Reply With Quote
Old 07-31-2007, 12:32 PM   PM User | #6
_Aerospace_Eng_
Supreme Master coder!


 
_Aerospace_Eng_'s Avatar
 
Join Date: Dec 2004
Location: In a place far, far away...
Posts: 19,293
Thanks: 2
Thanked 1,044 Times in 1,020 Posts
_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light
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!||||
_Aerospace_Eng_ is offline   Reply With Quote
Old 07-31-2007, 02:28 PM   PM User | #7
CFMaBiSmAd
Senior Coder

 
CFMaBiSmAd's Avatar
 
Join Date: Oct 2006
Location: Denver, Colorado USA
Posts: 2,712
Thanks: 2
Thanked 251 Times in 243 Posts
CFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the rough
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.
__________________
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..
CFMaBiSmAd is online now   Reply With Quote
Old 07-31-2007, 03:17 PM   PM User | #8
CFMaBiSmAd
Senior Coder

 
CFMaBiSmAd's Avatar
 
Join Date: Oct 2006
Location: Denver, Colorado USA
Posts: 2,712
Thanks: 2
Thanked 251 Times in 243 Posts
CFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the rough
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
CFMaBiSmAd is online now   Reply With Quote
Old 07-31-2007, 03:23 PM   PM User | #9
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,890
Thanks: 5
Thanked 79 Times in 78 Posts
firepages will become famous soon enough
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)
firepages is offline   Reply With Quote
Old 07-31-2007, 07:11 PM   PM User | #10
xxkylexx
New Coder

 
Join Date: Apr 2006
Posts: 50
Thanks: 2
Thanked 0 Times in 0 Posts
xxkylexx is an unknown quantity at this point
I solved this by just embedding the image into a page via HTML.
xxkylexx is offline   Reply With Quote
Old 07-31-2007, 09:56 PM   PM User | #11
rafiki
Senior Coder

 
rafiki's Avatar
 
Join Date: Aug 2006
Location: Floating around somewhere...
Posts: 2,034
Thanks: 18
Thanked 42 Times in 42 Posts
rafiki will become famous soon enough
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
__________________
Get Firefox Now
rafiki is offline   Reply With Quote
Old 07-31-2007, 11:44 PM   PM User | #12
timgolding
Senior Coder

 
timgolding's Avatar
 
Join Date: Aug 2006
Location: Southampton
Posts: 1,460
Thanks: 89
Thanked 110 Times in 109 Posts
timgolding is on a distinguished road
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.
timgolding is offline   Reply With Quote
Old 07-31-2007, 11:52 PM   PM User | #13
_Aerospace_Eng_
Supreme Master coder!


 
_Aerospace_Eng_'s Avatar
 
Join Date: Dec 2004
Location: In a place far, far away...
Posts: 19,293
Thanks: 2
Thanked 1,044 Times in 1,020 Posts
_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light
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!||||
_Aerospace_Eng_ is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:19 PM.


Advertisement
Log in to turn off these ads.