9

I have a page in my site, displaying some images that are produced my PHP. When I right click on an image and click Save Image As I get as default name the name of the php file used for generating the image.

This is for example the html for the image :

 <img src="picture_generator.php?image_id=5&extension=.png">

and the name I get is: picture_generator.php.png

Is there a way to set this name to a default one?

Thanks in advance

novellino
  • 1,069
  • 4
  • 22
  • 51

3 Answers3

13

You can provide it in the Content-Disposition HTTP header:

header('Content-Type: image/png');
header('Content-Disposition: inline; filename="' . $filename . '"');

However, some browsers (namely Internet Explorer) are likely to ignore this header. The most bullet-proof solution is to forge the URL and make the browser believe it's downloading a static file like /images/5/foo.png while the actual path behind the scenes is /picture_generator.php?image_id=5&extension=.png. This can be accomplished by some web server modules like Apache's mod_rewrite.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • Thanks for the replay. I tried this but did not work. Attachment instead on inline fits better. – novellino Jun 28 '11 at 14:26
  • This works great for Android's browser. I was having trouble saving images with the .php extension. This fixed it. – MrZander Jun 28 '13 at 22:40
  • This should be marked as the correct answer. While the content disposition of "attachment" will bring up the browser's download dialog window, the "inline" value avoids this and still provides the desired file name if a user right clicks on an image and clicks "Save As". Side note, I'd strongly discourage people from using mod_rewrite as this brings about possible risks. Most browsers these days respect this particular header. – Art Geigel Sep 15 '16 at 05:07
4

You can try to set the file name using the HTTP headers but not all browsers respect that.

The simplest trick is to extend the URL so that the last part contains the desired file name:

<img src="picture_generator.php/desiredfilename.jpg?image_id=5&extension=.png&name=desiredfilename.jpg">

Note I also added the file name at the end of the query string (the name doesn't really matter) as some browsers use that part.

Depending on your server configuration this will immediately work without any special configuration (no mod_rewrite or anything like that). You can check if it works on your server by simply appending "/foo" to any PHP-URL on your site. If you see the output of your PHP, all is good. If you see a 404 error then your server configuration can't deal with such URLs.

Udo G
  • 12,572
  • 13
  • 56
  • 89
  • Oups sorry my fault. It works fine to all browsers. Thanks a lot!! – novellino Jun 28 '11 at 14:39
  • Keep in mind that this solution may break when you move your code to another server as it depends on how the Web server and the PHP interpreter are working together. FYI, in your PHP script you can find that suffix in the `$_SERVER["PATH_INFO"]` variable [[docs](http://php.net/manual/en/reserved.variables.server.php)] – Udo G Jun 28 '11 at 21:42
  • adding an extra parameter still causes ie10 to just show untitled.jpg for me, maybe it's just what IE has done over the two years – Assimilater Aug 13 '13 at 20:23
  • @Assimilater: The *essential* part of my answer is tho extend the URL before the "?", i.e. `picture_generator.php/desiredfilename.jpg`, not just the query string. – Udo G Sep 04 '13 at 08:54
  • oh, so having image.php?[insert query string] would have been my problem then. – Assimilater Sep 09 '13 at 15:52
1

In your picture_generator.php file you need to add a header with the name. such as

header("Content-Disposition: attachment; filename=\"myfile.png\""); 
BugFinder
  • 17,474
  • 4
  • 36
  • 51
  • 1
    Are you sure it should be "attachment"? Wouldn't that cause the browser to try to download the file instead of displaying it? – Crashspeeder Jun 28 '11 at 14:13
  • sorry, was copying from my download app.. I meant to remove it. no, attchment forces download, but otherwise, it should work (once removed) – BugFinder Jun 28 '11 at 14:14
  • Actually it works for all the browsers but not for Safari. Does anyone have an idea why? Should I do something special? – novellino Jun 28 '11 at 14:22