Scenario: (in PHP) I have a form submission with a UTF-8 encoded string ($name
) to support international characters. Upon submitting the form (via GET), I am creating a CSV download file. I want the name of the file to be that string + .csv
("$name.csv"
). For a western character set I can do this just fine by doing:
header("Content-Disposition: attachment; filename=\"$name\"");
But for other character sets, the download file's name is garbage letters + .csv
(such as ×œ×œ× ×›×•×ª×¨×ª.csv
). I am trying to follow RFC 2231 to do something like:
header("Content-Disposition: attachment; filename*=UTF-8''$name");
But I seem to have a couple problems:
I need to encode each character of $name
octets encoded in hexadecimal, like "This%20is%20%2A%2A%2Afun%2A%2A%2A
". Does anyone have a function to do this properly? I coded the following but I don't think it is right:
$fileName = encodeWordRfc2231($name) . ".csv";
header("Content-Disposition: attachment; filename*=UTF-8''$fileName");
function &encodeWordRfc2231($word) {
$binArray = unpack("C*", $word);
foreach ($binArray as $chr) {
$hex_ary[] = '%' . sprintf("%02X", base_convert($chr, 2, 16));
}
return implode('', $hex_ary);
}
Does anyone out there have experience with this and can set me on the right path?
It is enough to encode the file name according to RFC 3986 by using rawurlencode()
So all you need to do is change the header() line to:
header("Content-Disposition: attachment; filename*=UTF-8''".rawurlencode($name));
To answer the questions directly: