强制下载带有外文字符的文件


Force downloading files with foreign characters

如何强制下载文件名中字符数大于英文的文件,例如瑞典语、挪威语,如ö ä 。如果文件中没有ö ä、但如果文件名中包含瑞典字符,则不能正常工作。

下面是我用来打开文件的代码:

$file_id = $_GET['f'];
$sql =  " SELECT * ".
        " FROM attachment ".
        " WHERE attachment_id = ".$file_id." ".
        $res = mysql_query($sql);
        $row = mysql_fetch_array($res);
        $filename = $row['filename'];
        $USER_ID = $row['user_id'];
        $DIRECTORY_ID = $row['directory_id'];
        $target_path = "upload/".$USER_ID."/".$DIRECTORY_ID."/";

// And the function is :
function Download($path, $speed = null)
{
    if (is_file($path) === true)
    {
        $file = @fopen($path, 'rb');
        $speed = (isset($speed) === true) ? round($speed * 1024) : 524288;
        if (is_resource($file) === true)
        {
            set_time_limit(0);
            ignore_user_abort(false);
            while (ob_get_level() > 0)
            {
                ob_end_clean();
            }
            header('Expires: 0');
            header('Pragma: public');
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header('Content-Type: application/octet-stream');
            header('Content-Length: ' . sprintf('%u', filesize($path)));
            header('Content-Disposition: attachment; filename="' . basename($path) . '"');
            header('Content-Transfer-Encoding: binary');
            while (feof($file) !== true)
            {
                echo fread($file, $speed);
                while (ob_get_level() > 0)
                {
                    ob_end_flush();
                }
                flush();
                sleep(1);
            }
            fclose($file);
        }
        exit();
    }
    return false;
    }
        Download($target_path.$filename);

我用过:

$filename = urlencode($filename); 
$filename =  htmlentities($filename, ENT_QUOTES, "UTF-8");
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

$path = iconv('UTF-8', 'ASCII//TRANSLIT', $path) 

但是什么都不起作用。我想提前感谢你,任何你能给的帮助将是非常感激的。乔纳斯。

use this:

<?php
header('Content-Type: text/html; charset=utf-8');
?>

并确保将PHP源文件保存为utf-8格式