不幸的是,目前還沒有一種適用於所有瀏覽器的解決方案。 至少有三個「更明顯」的方法來解決這個問題。
a)Content-type: application/octet-stream; charset=utf-8
+ filename=<utf8 byte sequence>
例如, filename=Москва.txt
這是違反標準,但Firefox正確顯示名稱。 IE沒有。
B)Content-type: application/octet-stream; charset=utf-8
+ filename=<urlencode(utf8 byte sequence)>
例如filename=%D0%9C%D0%BE%D1%81%D0%BA%D0%B2%D0%B0.txt
這適用於IE瀏覽器,但不適用於Firefox。
c)根據在rfc 2231
指定提供該名稱e.g filename*=UTF-8''%D0%9C%D0%BE%D1%81%D0%BA%D0%B2%D0%B0.txt
再次Firefox支持這一點,即,不。
一個更全面的比較看http://greenbytes.de/tech/tc2231/
編輯:當我說,沒有單一的解決方案,通過我的意思頭(「...」)。但是有一些工作要做。
當沒有可用的文件名時= xyz標頭瀏覽器使用url的路徑部分的基本名稱。即對於<a href="test.php/lala.txt">
Firefox和IE都建議使用lalala.txt
作爲文件名。
你可以追加額外的路徑組件後,實際路徑到您的PHP腳本(當使用Apache的httpd見http://httpd.apache.org/docs/2.1/mod/core.html#acceptpathinfo)。
例如如果您的文檔根目錄中有一個文件test.php,並將其請求爲http://localhost/test.php/x/y/z
,則變量$_SERVER['PATH_INFO']
將包含/x/y/z
。現在
,如果你把一個鏈接像
<a
href="/test.php/download/moskwa/Москва"
>
Москва
</a>
您的文檔中,你可以獲取download/moskwa/...
部分並開始文件的下載。不發送任何文件名= ...信息Firefox和IE均提示「正確」的名稱。
你甚至可以將它與根據rfc 2231發送名稱相結合。這就是爲什麼我也把moskwa
放入鏈接。這將是腳本用來查找它應該發送的文件的id。 IE忽略filename*=...
信息並仍然使用url的基本名稱部分來建議名稱。這意味着對於Firefox(和任何其他支持RFC 2231的客戶端),id之後的部分沒有意義*,但對於IE(和其他不支持rfc 2231的客戶端),它將用於名稱建議。
自足例如:
<?php // test.php
$files = array(
'moskwa'=>array(
'htmlentities'=>'Москва',
'content'=>'55° 45′ N, 37° 37′ O'
),
'athen'=>array(
'htmlentities'=>'Αθήνα',
'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'];
}
*)這是一個有點像在這裏堆棧溢出。的鏈接,這個問題被顯示爲
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
請添加一些基本信息。 '$ fileString'從哪裏來?它是什麼編碼? – 2010-04-05 12:36:14
變量從數據庫中挑選並使用。 instance_name和filestring – pks83 2010-04-05 12:46:40