0

下载.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. 如果我回显 $filename,我会看到正确的希腊名称,所以我认为正确的名称是在我的 download.php 中传递的。

  3. 由于我使用 $filename 获得了正确的名称并且我的实际文件具有相同的名称,因此脚本在哪里无法下载我的文件并且它给了我错误消息?

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

4

1 回答 1

2

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

要在标头中发送非 ASCII 符号,它们需要根据 RFC 2231 进行编码。
请在此处查看此答案:如何根据 RFC 2231 在 PHP 中编码文件名?

于 2012-11-22T07:18:37.623 回答