使用瑞典字符(åäö)文件名强制下载


Force download with Swedish characters (åäö) filename

我使用以下代码和函数来强制下载文件,如果文件名不包含瑞典字符(如Å Ä Ö),它会工作得很好。

$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."/";

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);

我试着把这个放在我的页面顶部:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

:

$filename = urlencode($filename); 

或:

$filename =  htmlentities($filename, ENT_QUOTES, "UTF-8");

但还是一样的问题,我打不开。但是如果文件名包含正常的英文字符,那么它就可以正常工作。

你有什么建议我可以把或实现的功能?

确保html-document中有utf8编码(<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>)

尝试替换

$filename = $row['filename'];

$filename = iconv("UTF-8", "ISO-8859-1//TRANSLIT", $row['filename']);   
所以你的代码看起来像:
$file_id = $_GET['f'];
$sql =  " SELECT * ".
            " FROM attachment ".
            " WHERE attachment_id = ".$file_id." ".
            $res = mysql_query($sql);
            $row = mysql_fetch_array($res);
            $filename = iconv("UTF-8", "ISO-8859-1//TRANSLIT", $row['filename']);   
            $USER_ID = $row['user_id'];
            $Directory_id = $row['directory_id'];
            $target_path = "upload/".$USER_ID."/".$Directory_id."/";

但是为了它。不要使用mysql()-functions。使用mysql或PDO代替占位符。您的代码广泛用于sql注入攻击。