3

Possible Duplicate:
How to change the filename displayed in the “Save as…” dialog from .php to .png

My PHP-generated GUI displays a dynamically-generated PNG image:

<img src="someScript.php?dataId=xyz&width=250&height=200" alt="Chart" />

The image contents are dynamic and unique based on the dataId argument:

<?php
header('Content-Type: image/png');
// output image based on $_GET['dataId']
?>

But when my users try to right-click and Save As in IE, Firefox or Chrome, the suggested filename is someScript.php.png. This is non-unique and not very useful.

How can I "hint" to browsers as to what the "save as" filename should be?

Browsers can always do what they like, but the precedent set by HTTP's Content-Disposition: attachment; filename=<filename> implies to me that there may be some way of hinting at a filename. This HTTP header itself is not appropriate because the I want the Content-Disposition to remain at the default of inline.

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055

1 Answers1

9

The filename argument is also supported for Content-Disposition: inline:

[RFC6266]: 4.3. Disposition Parameter: 'Filename'

The parameters "filename" and "filename*", to be matched case-insensitively, provide information on how to construct a filename for storing the message payload.

Depending on the disposition type, this information might be used right away (in the "save as..." interaction caused for the "attachment" disposition type), or later on (for instance, when the user decides to save the contents of the current page being displayed).

So:

<?php
$filename = "image_" . $_GET['dataId'] . ".png";

header("Content-Type: image/png");
header("Content-Disposition: inline; filename=\"{$filename}\"");

// output image based on $_GET['dataId']
?>
Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055