When downloading file names from non-English languages ​​do not appear correctly in the downloaded file

When I try to download a file whose name has characters from languages ​​such as Chinese Japanese, etc ....... not ascii ... the name of the downloaded file is garbled. How to fix it.

I tried putting charset = UTF-8 in the Content-type header property, but failed. Please help. The code is below.

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

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

heading

("Content Type: Application / Octet Stream");

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

header ("Content-Length:" (string) (file size ($ fileString)).);

sleep (1);

fpassthru ($ FDL);

+5
source share
2 answers

Unfortunately, there is currently no solution that works with all browsers. There are at least three “more obvious” approaches to this issue.

a) Content-type: application/octet-stream; charset=utf-8+ filename=<utf8 byte sequence>
for example filename=.txt
This is a violation of the standards, but firefox shows the name correctly. IE does not.

b) Content-type: application/octet-stream; charset=utf-8+ filename=<urlencode(utf8 byte sequence)>
for example filename=%D0%9C%D0%BE%D1%81%D0%BA%D0%B2%D0%B0.txt
This works with IE, but not firefox.

c) providing the name specified in rfc 2231
for example, filename*=UTF-8''%D0%9C%D0%BE%D1%81%D0%BA%D0%B2%D0%B0.txt
Firefox again supports this, IE does not.

for a more complete comparison see http://greenbytes.de/tech/tc2231/


edit: , , header ('...'). - .
filename = xyz, URL-. <a href="test.php/lala.txt"> firefox, IE lalala.txt .
php script ( 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/&#x41c;&#x43e;&#x441;&#x43a;&#x432;&#x430;"
>
  &#x41c;&#x43e;&#x441;&#x43a;&#x432;&#x430;
</a>

download/moskwa/... . - filename =... Firefox IE "" .
rfc 2231. moskwa . , script , . IE filename*=... - URL-, . , firefox ( , rfc 2231) id *, IE ( , rfc 2231) .
:

<?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'];
}

*) .

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

+9

, , .

- , - XP , , .

, , .

, .

0

All Articles