简体   繁体   English

PHP Content-Disposition header:文件名中的特殊字符

[英]PHP Content-Disposition header: Specialcharacters in the filename

I am trying to download a file with php that has special characters in the file name.我正在尝试下载文件名中包含特殊字符的 php 文件。 Unfortunately these characters are substituted by "_".不幸的是,这些字符被“_”代替了。

The code:代码:

$filename = trim('<>$%&/=?' . '.txt');

header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
header("Cache-Control: public");
header("Content-Type: application/text");
header("Content-Transfer-Encoding: Binary");
header('Content-Disposition: attachment; '
        . sprintf('filename="%s"; ', urlencode($filename))
        . sprintf("filename*=utf-8''%s", urlencode($filename)));
echo "file contents";
die();

returns a file named返回一个名为

"__$%&_=_.txt"

I have tried different variations with urlencode(), rawurlencode(), mb_convert_string($filename, "UTF-8").我尝试了 urlencode()、rawurlencode()、mb_convert_string($filename, "UTF-8") 的不同变体。 The already present issues Special Characters in Content-Disposition filename , How to encode the filename parameter of Content-Disposition header in HTTP?已经存在的问题Special Characters in Content-Disposition filename如何将Content-Disposition header的filename参数编码为HTTP? and PHP: RFC-2231 How to encode UTF-8 String as Content-Disposition filename did not really help me.PHP:RFC-2231 如何将 UTF-8 字符串编码为 Content-Disposition 文件名并没有真正帮助我。 Do you have any further ideas?你有什么进一步的想法吗?

Encoding of the file is UTF-8.该文件的编码为 UTF-8。

Sprintf() seems not to be the problem: Sprintf() 似乎不是问题所在:

header("Content-Disposition: attachment; filename*=utf-8''" . rawurlencode($filename)); 

or或者

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

give the same result.给出相同的结果。

I believe, the issue has nothing to do with the encoding of the filename ( "Content-Disposition: attachment; filename*=utf-8''" . rawurlencode($filename) option seems correct).我相信,这个问题与文件名的编码无关( "Content-Disposition: attachment; filename*=utf-8''" . rawurlencode($filename)选项似乎是正确的)。

Some characters such as ?一些字符,例如? , < , > are just not allowed in filenames on particular platforms and replaced with _ by a browser. , < , >只是不允许在特定平台上的文件名中使用,并由浏览器替换为_

Also, you can have both filename and filename* params in Content-Disposition header, using the former as a fallback for client that doesn't support extended filename* param.此外,您可以在Content-Disposition标头中同时包含filenamefilename*参数,将前者用作不支持扩展filename*参数的客户端的后备。 It can be done seamlessly with content-disposition lib.它可以通过content-disposition库无缝完成。

你试过这个吗?

header("Content-Disposition: attachment; filename=".utf8_decode(htmlspecialchars_decode($filename))); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 php标头内容处置 - php header Content-disposition Firefox将下载retrieve.php,而不是每个Content-Disposition标头指定的文件名 - Firefox downloads retrieve.php instead of filename given per Content-Disposition header 如何更正Content-Disposition标头中的文件名值? - How to correct filename value in Content-Disposition header? 使用 PHP 中的“content-disposition:attachment”标头获取请求的内容 - Get content of a request with 'content-disposition: attachment' header in Php PHP标头内容 - 处置:附件强制.php文件或内联 - PHP Header Content-Disposition: attachment forces .php file or inline PHP标题:内容处理:附件,然后位置:一些网址 - PHP Header : Content-disposition: attachment , then Location: some url 标头(“ Content-Type:应用程序/ zip”)和标头(“ Content-Disposition:附件;文件名= $ fileName”)在wordpress中不起作用? - header(“Content-Type: application/zip”) and header(“Content-Disposition: attachment; filename=$fileName”) not working in wordpress? 客户端缺少内容处理标头 - Content-Disposition header missing at client 如何在Content-Disposition标头中使用带有特殊字符的文件名? - How can I use a filename with special characters in a Content-Disposition header? 如何使用 content-disposition 标头设置下载的 UTF-8 文件名? - How to use the content-disposition header to set a downloaded UTF-8 filename?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM