Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP内容处置标头:文件名中的特殊字符_Php_Encoding_Http Headers - Fatal编程技术网

PHP内容处置标头:文件名中的特殊字符

PHP内容处置标头:文件名中的特殊字符,php,encoding,http-headers,Php,Encoding,Http Headers,我试图下载一个带有php的文件,文件名中有特殊字符。 不幸的是,这些字符被替换为“\” 守则: $filename = trim('<>$%&/=?' . '.txt'); header($_SERVER["SERVER_PROTOCOL"] . " 200 OK"); header("Cache-Control: public"); header("Content-Type: application/text"); header("Content-Transfer-Enc

我试图下载一个带有php的文件,文件名中有特殊字符。 不幸的是,这些字符被替换为“\”

守则:

$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();
我尝试了urlencode()、rawurlencode()、mb_convert_字符串($filename,“UTF-8”)的不同变体。 已经存在的问题,并没有真正帮助我。 你还有什么进一步的想法吗

文件的编码为UTF-8

Sprintf()似乎不是问题所在:

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


给出相同的结果

sprintf()
等都不支持unicode。它们将损坏多字节文本。sprintf也没有mb_*()等价物。您必须使用其他方式。谢谢您的输入。但是,sprintf()似乎不是问题所在。标题(“内容处置:附件;文件名*=utf-8”“”。rawurlencode($filename));或标题(“内容处置:附件;文件名=\”.$filename.\”;给出相同的结果。文件名仍然是“$%&=.txt”。它还尝试了mb_convert_encoding()等不同的变体。在我找到更好的方法之前,似乎最方便的事情是实际音译。
header("Content-Disposition: attachment; filename*=utf-8''" . rawurlencode($filename)); 
header("Content-Disposition: attachment; filename=\"" . $filename . "\"");