内容目录
之前写一个php下载,然后想到了如果下载中文文件我们该怎么办呢。整理了一下代码在linux上跑,通过了firefox,chrome,ie11测试。基本可用。
废话不多话,上代码
<?php function get_basename($filename){ return preg_replace('/^.+[\\\\\\/]/', '', $filename); } $file = "/tmp/你好.txt"; $filename = get_basename($file); header("Content-type: application/octet-stream"); //处理中文文件名 $ua = $_SERVER["HTTP_USER_AGENT"]; $encoded_filename = urlencode($filename); $encoded_filename = str_replace("+","%20",$encoded_filename); if (preg_match("/MSIE/", $ua)) { header('Content-Disposition: attachment; filename="' . $encoded_filename . '"'); } else if (preg_match("/Firefox/", $ua)) { header("Content-Disposition: attachment; filename*=\"utf8''" . $filename . '"'); } else { header('Content-Disposition: attachment; filename="' . $filename . '"'); } header("Content-Length: ". filesize($file)); readfile($file);