Frage

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

War es hilfreich?

Lösung

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

Andere Tipps

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');
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top