【问题标题】:utf-8 character recognition issueUTF-8 字符识别问题
【发布时间】:2012-11-10 11:53:04
【问题描述】:

下载.php

} else {
    $filename = NULL;
}

$err = '<div align="center">GREEK error msg</div>';

if (!$filename) {
    // if variable $filename is NULL or false display the message
    echo $err;
} else {
    // define the path to your download folder plus assign the file name
    $path = '../downloads/'.$filename;

    // check that file exists and is readable
    if (file_exists($path) && is_readable($path)) {
        // get the file size and send the http headers
        $size = filesize($path);
        header('Content-Type: application/octet-stream;');
        header('Content-Length: '.$size);
        header('Content-Disposition: attachment; filename='.$filename);
        header('Content-Transfer-Encoding: binary');
        // open the file in binary read-only mode
        // display the error messages if the file can´t be opened
        $file = @ fopen($path, 'rb');
        if ($file) {
            // stream the file and exit the script when complete
            fpassthru($file);
            exit;
        } else {
            echo $err;
        }
    } else {
        echo $err;
    }
}
?>

我就是这样称呼它的:

<a href="scripts/download.php?file=GREEKCHARS_Earth.pdf"></a>
  1. 如果文件名是英文,下载脚本可以正常工作。
    如果文件名是希腊语,则会显示错误消息。

  2. 如果我 echo $filename 我看到了正确的希腊名称,所以我认为正确的名称是在我的 download.php 中传递的。

  3. 由于我得到 ​​$filename 的正确名称,并且我的实际文件具有相同的名称,脚本在哪里无法下载我的文件并给出错误消息? p>

似乎无法将希腊语 $filename 与实际文件匹配。

【问题讨论】:

  • 试试header('Content-Disposition: attachment; filename='.rawurlencode($filename));
  • 这有很多地方会搞砸。首先,服务器接收到的值是否正确?尝试bin2hex()ing $_GET 值并检查这些是否是“ΔΨΞ”(CE94CEA8CE9E)的实际 UTF-8 字节。
  • @deceze echo bin2hex($_GET) . "&lt;br /&gt;"; 什么也不输出。 echo bin2hex($filename) . "&lt;br /&gt;"; 输出:cea42ece942e5f3039385f
  • 产生cea42ece942e5f3039385f的字符串是什么?
  • 好的。 Τ.Δ._003_GammaRAE_II_R 输出 cea42ece942e5f3030335f47616d6d615241455f49495f522e706466

标签: php intranet


【解决方案1】:

问题在于 HTTP 标头可能只包含 ASCII 字符。这是标准,因为标头用于定义以何种编码遵循的内容,因此标头本身不能包含某些尚未指定编码的字符。

要在标头中发送非 ASCII 符号,它们需要根据 RFC 2231 进行编码。
在此处查看此答案:How can I encode a filename in PHP according to RFC 2231?

【讨论】:

  • 嗯..我从来没有见过这个。你能解释一下在哪里/如何使用它吗?谢谢。
  • bin2hex() 用于$filename 时,我得到了utf-8 结果。但是&lt;a href="scripts/download.php?**file=GREEKCHARS_Earth.pdf**"&gt; 将文件名发送为 !#!#!#_EARTH.pdf 这有什么不同吗?
  • 而不是Content-Disposition: attachment; filename=ΔΨΞ,根据HTTP规范这是非法的,你应该发送Content-Disposition: attachment; filename=title*0*=utf-8...之类的东西。所以只需修改你设置header()的那一行。
  • “将文件名发送为...”是什么意思?
  • Firebug 将上述具有 file=GREEKCHARS_Earth.pdf 的 url 显示为 file=!#!#!#_EARTH.pdf 所以我在问如果有可以放置的php代码使url得到正确的格式。
猜你喜欢
  • 1970-01-01
  • 2016-07-12
  • 2013-07-04
  • 2011-04-19
  • 1970-01-01
  • 2010-09-27
  • 1970-01-01
  • 2011-11-04
  • 1970-01-01
相关资源
最近更新 更多