2

To password protect the website images, I access them through PHP. Intead of using the url "meme_penguin.jpg" I use "image.php?file=penguin". This works great for displaying it, except when the user try to "save as".

In chrome the "save as" dialogue will suggest the file type as PHP instead of JPG In internet explorer it will suggest BMP

Obviously the user can just change the file type and rename the file. However this is not an elegant solution and will cause problems for sure.

How can we make it automatically "save as" with the proper extension, JPG? Is there an alternative way? - Many thanks!!

Calvin
  • 194
  • 17

2 Answers2

2

Found it! I was missing the third line of code. You can use this to display a secure image in your page, or download it as jpg Got the first two lines from another question in stackoverflow

//need this two lines to work properly on IE8
header("Pragma: "); 
header("Cache-Control: ");

//was missing this line
header('Content-Disposition: attachment; filename="renamed.jpg"');

header("Content-Type: image/jpeg");
echo file_get_contents("images/somepic.jpg");

from php.net

Calvin
  • 194
  • 17
0

Set the content-type header to proper value in response

http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html 14.17 Content-Type

header('Content-Type: image/jpeg');
cske
  • 2,233
  • 4
  • 26
  • 24
  • have you tried this @cske? because it seems you haven't. this is what I wrote to display the image in the first place, and it still "saves as" PHP instead of JPG!!! – Calvin Sep 16 '13 at 12:40
  • You failed to include the code you've tried yet. You may searching for this Content-Disposition header http://www.ietf.org/rfc/rfc2183.txt, or use rewrite rules to serve images with php – cske Sep 16 '13 at 12:55
  • Well @cske I figure it out by myself that failed! What I was really looking for was actually helpful help. Anyway I figure it out eventually, but can't post answer because I have to wait 5 hours.... – Calvin Sep 16 '13 at 14:30