View Full Version : invoking "Save Target As" dynamically on an image
pythone
03-03-2004, 03:21 PM
Hello
I was wondering whether it is possible to invoke the "Save Target As" option (the one accessed via right-clicking a file) on an image in a page.
That is, if the user clicks on a link/button the behaviour should be as if the user right-clicked an image and chose "Save Target As" (on IE of course), i.e. open the save file dialog.
thanks
Yes, it is... create a php page that sets the content type to something that wouldn't be opened by the browser, read from the image file and send it to the client.
There is a problem, though... Internet Explorer incorrectly handles the Content-Type header and relies on the document's extension before the Content-Type... nothing serious as most users won't have the extension .PHP registered, but don't be surprised if the result on your machine isn't what's expected.
There is a Microsoft fix... not sure if it's integrated in the newest versions of IE however.
Your best solution is probably to give users instructions on how to store the image... using PHP would mean the image would be requested from the server twice, whereas right clicking the image in IE and selecting 'Save Picture As' can simply copy the image from the local cache.
daddyroy
03-21-2005, 06:33 PM
This is very possible using PHP:
1) create a file called "download.php" that contains the following:
<?php
if ($_REQUEST ['getfile']){
$file = $_REQUEST ['getfile'];
} else {
$file = "..";
}
if(strstr($file, "..")){
Header( "Content-Type:
text/html");
echo ('
<html><head><title>Error</title></head><body>
<h3>Invalid characters in
filename.</h3>
</body></html>
');
} else if(!file_exists($file) ||
!is_readable($file)){
Header ("Content-Type: text/html");
echo ('
<html><head><title>Error</title></head><body>
<h3>The file you requested does not
exist.</h3>
</body></html>
');
} else {
Header ("Content-Type: audio/wma");
Header ("Content-Length: ".filesize($file));
Header ("Content-Disposition: attachment;
filename=\"$file\"");
Hea
der ("Content-Description: Windows Media
Audio File");
readfile($file);
}
?>
2) Place this file in you image folder
3) Create your links on the image page with the folowing format:
<a href="PATH TO YOUR IMAGE FOLDER/download.php?getfile=IMAGE_NAME(Ex. yellow_flower.jpg)">Flower</a>
That's it.
vBulletin v3.0.3, Copyright ©2000-2012, Jelsoft Enterprises Ltd.