I am using PHP and javascript/jQuery for my project. I want to change the filename for the save as dialog when the user right clicks on an image and selects save as. (for example i want to name every "save dialog" filename "image.png"). Thanks in advance.
Asked
Active
Viewed 655 times
0
-
The right click menu and the default filename are controlled by the browser and you cannot have any influence over them (other than changing the filename in the URL of course). – Jon Jul 02 '13 at 09:55
-
thanks. I'll try to think of a different way. – Karter Jul 02 '13 at 10:06
-
@Jon I'm pretty sure you can prevent the default behaviour on mouse click and do your own thing – slash197 Jul 02 '13 at 10:10
-
1@slash197: You can, but that's horrible practice and I am going to pretend that it does not exist. Leave the web alone! ;-) Seriously now, cancelling the browser's menu and displaying your own in JS would not help with this problem. – Jon Jul 02 '13 at 10:14
1 Answers
2
The file name that the browser gives in the save dialog CAN be changed, but not in Javascript. Usually this is done when you don't give a direct link to an image, but use a PHP script such as < img src="image.php?id=18" >
To do this you just need to send proper http headers in image.php, for example:
header('Content-type: image/jpg');
header('Content-Disposition: inline; filename="' . $filename . '"');
If you use non-ASCII file name, you will need to encode it and unfortunately the encoding is different for different browsers:
- For IE use rawurlencode($filename)
- For FF use base64 with charset specification, such as '=?UTF-8?B?'.base64_encode($filename).'?='
If you specify "attachment" instead of "inline" in Content-Disposition header, the browser will not try to display the image but will immediately prompt user to download it.

astax
- 1,769
- 1
- 14
- 23