跟数据库(存的是文件名和文件编号)和物理路径结合起来的,上传上去的文件没有问题,可是下载下来,如果文件内容有中文的话,下载下来文件(如 txt doc...)内容就会是乱码,英文和数字没有问题  $f_open = fopen($file_path,"r");

$ua = $_SERVER['HTTP_USER_AGENT'];
$encodedfilename = urlencode($file_path);  //文件的编码格式
$encoded_filename = str_replace("+", "%20", $encodedfilename);
header("Content-Type: application/x-octet-stream;charset=gb2312"); //输出文件类型
header("Accept-Ranges: bytes"); 
header("Accept-Length: ".filesize($file_path)); 

if(preg_match('/MSIE/',$ua)){
header('Content-Disposition: attachment; filename="'.$encoded_filename.'"');/制下载
print_r("IE浏览器");
 }else if(preg_match('/Firefox/',$ua)){
header('Content-Disposition: attachment; filename*="utf8\'\''.$file_path.'"');
print_r("火狐浏览器");
 }else{
header('Content-Disposition: attachment; filename="'.$file_path.'"');
print_r("未知浏览器");
 }
// 输出文件内容
echo readfile($file_dir.iconv('utf-8','gb2312//ignore',$filename));

 fclose($f_open);
exit;

解决方案 »

  1.   

    header("Content-Type: application/x-octet-stream;charset=gb2312"); //输出文件类型改为header("Content-Type: application/x-octet-stream;charset=utf-8"); //输出文件类型建议限定类型为utf-8
      

  2.   

    echo readfile($file_dir.iconv('utf-8','gb2312//ignore',$filename));
    如果文件本身就是gbk的那么你这句话中用到的iconv会出问题,如果你文件本身就是gbk的话你完全不必用iconv方法,如果你不会看文件的编码格式,你可以用echo strlen('我')来看看它的长度,如果是3说明文件是utf-8的,如果是2说明是gbk的