When I am trying to download a file whose name has characters from languages like Chinese Japanese etc…. non ascii… the downloaded file name is garbled. How to rectify it.
I have tried to put charset=UTF-8 in the Content-type header property, but no success. Code below.
header("Cache-Control: ");// leave blank to avoid IE errors
header("Pragma: ");// leave blank to avoid IE errors
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$instance_name."\"");
header("Content-length:".(string)(filesize($fileString)));
sleep(1);
fpassthru($fdl);
Unfortunately there is currently not a single solution that works with all browsers.
There are at least three “more obvious” approaches to the problem.
a)
Content-type: application/octet-stream; charset=utf-8+filename=<utf8 byte sequence>e.g.
filename=Москва.txtThis is a violation of standards but firefox shows the name correctly. IE doesn’t.
b)
Content-type: application/octet-stream; charset=utf-8+filename=<urlencode(utf8 byte sequence)>e.g.
filename=%D0%9C%D0%BE%D1%81%D0%BA%D0%B2%D0%B0.txtThis works with IE but not with firefox.
c) providing the name as specified in rfc 2231
e.g
filename*=UTF-8''%D0%9C%D0%BE%D1%81%D0%BA%D0%B2%D0%B0.txtAgain firefox supports this, IE doesn’t.
for a more comprehensive comparison see http://greenbytes.de/tech/tc2231/
edit: When I said that there is no single solution, I meant via header(‘…’). But there is something of a work around.
When there is no usable filename=xyz header browsers use the basename of the path part of the url. I.e. for
<a href="test.php/lala.txt">both firefox and IE suggestlalala.txtas the filename.You can append extra path components after the actual path to your php script (when using apache’s httpd see http://httpd.apache.org/docs/2.1/mod/core.html#acceptpathinfo).
E.g. if you have a file test.php in your document root and request it as
http://localhost/test.php/x/y/zthe variable$_SERVER['PATH_INFO']will contain/x/y/z.Now, if you put a link like
in your document you can fetch the
download/moskwa/...part and initiate the download of the file. Without sending any filename=… information both firefox and IE suggest the “right” name.You can even combine it with sending the name according to rfc 2231. That’s why I also put
moskwainto the link. That would be the id the script uses to find the file it is supposed to send. The IE ignores thefilename*=...information and still uses the basename part of the url to suggest a name. That means for firefox (and any other client that supports rfc 2231) the part after the id is meaningless* but for the IE (and other clients not supporting rfc 2231) it would be used for the name suggestion.self-contained example:
*) That’s a bit like here on Stack Overflow. The link for this question is shown as
but it also works with
the important part is the id
2578349