而从非英语语言下载文件名不会正确地显示在下载的文件上 [英] while downloading filenames from non english languages are not getting displayed on the downloaded file correctly

查看:89
本文介绍了而从非英语语言下载文件名不会正确地显示在下载的文件上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我正在尝试下载一个文件,其名称具有汉字等语言的字符......非ascii ...下载的文件名是乱码。如何纠正它。

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.

我已经尝试将charset = UTF-8放在Content-type标头属性中,但没有成功。请帮忙。

I have tried to put charset=UTF-8 in the Content-type header property, but no success. Please help. Code below.

标头(Cache-Control:); //留空以避免IE错误

header("Cache-Control: ");// leave blank to avoid IE errors

头(Pragma:); //留空以避免IE错误

header("Pragma: ");// leave blank to avoid IE errors

头(Content-type:application / octet-stream);

header("Content-type: application/octet-stream");

头(Content-Disposition:attachment; filename = \$ instance_name。\);

header("Content-Disposition: attachment; filename=\"".$instance_name."\"");

header(Content-length:。(string)(filesize($ fileString)));

header("Content-length:".(string)(filesize($fileString)));

sleep(1);

sleep(1);

fpassthru($ fdl);

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>

例如 filename =Москва.txt

这是违反标准,但firefox正确显示名称。 IE没有。

a) Content-type: application/octet-stream; charset=utf-8 + filename=<utf8 byte sequence>
e.g. filename=Москва.txt
This is a violation of standards but firefox shows the name correctly. IE doesn't.

b)内容类型:应用程序/八位字节流; charset = utf-8 + filename =< urlencode(utf8 byte sequence)>

例如 filename =%D0%9C%D0%BE%D1%81%D0%BA%D0%B2%D0%B0.txt

此作品与IE但不是与Firefox。

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.txt
This works with IE but not with firefox.

c)提供中指定的名称rfc 2231

例如 filename * = UTF-8%D0%9C%D0%BE%D1%81%D0%BA%D0%B2%D0 %B0.txt

再次firefox支持这一点,IE不会。

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.txt
Again firefox supports this, IE doesn't.

更全面的比较见< a href =http://greenbytes.de/tech/tc2231/ =noreferrer> http://greenbytes.de/tech/tc2231/

编辑:当我说没有一个解决方案时,我的意思是通过头('...')。但是有一些工作要做。

当没有可用的文件名= xyz标头浏览器使用url的路径部分的basename。即对于< a href =test.php / lala.txt> firefox和IE都建议 lalala.txt 作为文件名。

您可以在您的php脚本的实际路径后附加额外的路径组件(使用apache的httpd时,请参阅 http://httpd.apache.org/docs/2.1/mod/core.html#acceptpathinfo )。< br>
例如如果您的文档根目录中有一个文件test.php,并将其请求为 http://localhost/test.php/x/y/z 变量 $ _ SERVER ['PATH_INFO'] 将包含 / x / y / z

现在,如果你把一个链接,如

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 suggest lalala.txt as 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/z the variable $_SERVER['PATH_INFO'] will contain /x/y/z.
Now, if you put a link like

<a
  href="/test.php/download/moskwa/&#x41c;&#x43e;&#x441;&#x43a;&#x432;&#x430;"
>
  &#x41c;&#x43e;&#x441;&#x43a;&#x432;&#x430;
</a>

在您的文档中,您可以获取 download / moskwa / ... 部分,并启动文件的下载。没有发送任何文件名= ...信息,Firefox和IE都建议使用正确的名称。

您甚至可以根据rfc 2231发送名称。这也是为什么我也把 moskwa 进入链接。这将是脚本用来查找应该发送的文件的id。 IE忽略 filename * = ... 信息,仍然使用url的basename部分来建议一个名称。这意味着对于firefox(以及支持rfc 2231的任何其他客户端),id之后的部分是无意义的,但是对于IE(和其他不支持rfc 2231的客户端),它将被用于名称建议。

自包含的例子:

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 moskwa into the link. That would be the id the script uses to find the file it is supposed to send. The IE ignores the filename*=... 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:

<?php // test.php
$files = array(
  'moskwa'=>array(
    'htmlentities'=>'&#x41c;&#x43e;&#x441;&#x43a;&#x432;&#x430;',
    'content'=>'55° 45′ N, 37° 37′ O'
  ),
  'athen'=>array(
    'htmlentities'=>'&#x391;&#x3b8;&#x3ae;&#x3bd;&#x3b1;',
    'content'=>'37° 59′ N, 23° 44′ O'
  )
);


$fileid = null;
if ( isset($_SERVER['PATH_INFO']) && preg_match('!^/download/([^/]+)!', $_SERVER['PATH_INFO'], $m) ) {
  $fileid = $m[1];
}

if ( is_null($fileid) ) {
  foreach($files as $fileid=>$bar) {
    printf(
      '<a href="./test.php/download/%s/%s.txt">%s</a><br />', 
      $fileid, $bar['htmlentities'], $bar['htmlentities']
    );
  }  
}
else if ( !isset($files[$fileid]) ) {
  echo 'no such file';
}
else {
  $f = $files[$fileid];
  $utf8name = mb_convert_encoding($f['htmlentities'], 'utf-8', 'HTML-ENTITIES');
  $utf8name = urlencode($utf8name);

  header("Content-type: text/plain");
  header("Content-Disposition: attachment; filename*=UTF-8''$utf8name.txt");
  header("Content-length: " . strlen($f['content']));
  echo $f['content'];
}






*)这有点像这样堆栈溢出。此问题的链接显示为


*) That's a bit like here on Stack Overflow. The link for this question is shown as

http://stackoverflow.com/questions/2578349/while-downloading-filenames-from-non-english-languages-are-not-getting-displayed

但它也适用于

http://stackoverflow.com/questions/2578349/mary-had-a-little-lamb

重要的部分是id 2578349

这篇关于而从非英语语言下载文件名不会正确地显示在下载的文件上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆