技术文章
当前位置:首页 > 技术文章

php中UTF-8编码和中文文件下载问题

filelist.php内容如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
$files=dir("./");
$i=-1;
while(false!==($filename=$files->read())){
if(($filename=='.')||($filename=='..')){continue;}
$filename=iconv('gb2312','utf-8',$filename);
?>
<a href="download.php?filename=<?php print "$filename";?>"><?php print "$filename";?></a>
<br>
<?php
}
?>
</body>
</html>
download.php内容如下:
<?php
header("Content-Type:text/html;charset=utf-8");
$filename=$_GET["filename"];
$filename=iconv('utf-8','gb2312',$filename);
header("Content-type: application/octet-stream");
header("Accept-Ranges: bytes");
header('Content-Disposition: attachment; filename="' . $filename . '"');
$file = fopen($filename,"r");
echo fread($file,filesize($filename));
fclose($file);
?>