pdf使用utf 8下载

时间:2013-12-18 11:15:03

标签: pdf unicode utf-8 download

Helllo,

我hive pdf下载脚本:     

    $download_path = $_SERVER['DOCUMENT_ROOT'] . "/test";
    $filename = $_GET['filename'];

    if(!$filename) die("I'm sorry, you must specify a file name to download.");

    if(eregi("\.\.", $filename)) die("I'm sorry, you may not download that file.");
    $file = str_replace("..", "", $filename);

    if(eregi("\.ht.+", $filename)) die("I'm sorry, you may not download that file.");
    $file = "$download_path/$file";
    if(!file_exists($file)) die("I'm sorry, the file doesn't seem to exist.");
    $type = filetype($file);
    $today = date("F j, Y, g:i a");
    $time = time();
    header("Content-type: $type");
    header("Content-Disposition: attachment;filename=$filename");
    header('Pragma: no-cache');
    header('Expires: 0');
    readfile($file);
    ?>

和pdf搜索脚本:

 <?php 
    $directory = opendir('./test');
    $userid = "1487";
      while ($file = readdir($directory)) {
        if($file!="." && $file!=".." && strpos($file,$userid) !== false){
          echo '<a href="dl.php?filename='.$file.'">Download PDF</a><BR>';
        }
      }
    closedir($directory);
    ?>

Pdf文件名为1487LŪW,启动下载文件时,文件名更换为1487L W。并且文件找不到。可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

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

参数化标头值不能可靠地包含任何非ASCII字符。或者至少在每个人都支持RFC 5987之前。

作为一种解决方法,在此之前,省略filename标头中的Content-Disposition参数,并在创建文档链接时将文件名包含为尾随路径部分。例如:

http://example.com/dl.php/1487L%C5%AAW?filename=1487L%C5%AAW

浏览器将选择路径的最后一部分作为默认文件名。在这里包含UTF-8字符是比今天使用Content-Disposition标头更可靠的跨浏览器。

您还需要对您放入URL参数的任何字符进行URL编码,并对HTML属性(或文本内容)中包含的任何值进行HTML编码,因此将它们放在一起就需要一些东西像:

$url = "dl.php/'.rawurlencode($file).'?filename='.rawurlencode($file);
echo '<a href="'.htmlspecialchars($url).'">Download PDF</a>';

(如果你知道你将在那个变量可用的服务器上运行,你可以通过从$_SERVER['REQUEST_URI']而不是get-parameter读取尾随路径部分来减少重复。 )