Solve the problem of php Chinese file download
The following function is in PHP to determine whether the download file exists (including Chinese, English, etc.), the absence of the prompt, the presence of the original name on the server to download, of course, you can also customize the name.
First of all, the site directory is as follows:
/
..... down.html
..... Sphinx Technical Exchange. zip
.... Lib
??? ... download.php
?
Of course, if the relative path of the site and the absolute path understanding is familiar with, you can not understand the layout of the directory, directly look at the source program.
?
File name: down.html download page
<title>Download Center</title>
Sphinx Technical Exchange
?
The file name is: download.php processing the Download Program page
Header ("Content-type:text/html;charset=utf-8");
$FN = UrlDecode ($_request[' fn ');? To decode, file name (package extension)
Determine if the download name is original or custom
if (!empty ($_request[' Savefn ')) {
??? $savefn = UrlDecode ($_request[' savefn ');? To decode, the file name that is displayed when downloading (without package extension)
??? $FNARR = Explode ('. ', $FN);
??? $savefn = $savefn. ".". $FNARR [Count ($FNARR)-1];? Convert Utf-8 to GB2312
}else{
??? $savefn = $FN;? The name of the original file name as the download display
}
$FN = Iconv (' UTF-8 ', ' Gb2312//ignore ', $FN);? Convert Utf-8 to GB2312
$fp = '. /'. $fn;? The path of the downloaded file, if changed to $fp = ' http://localhost/'. $fn; There will be problems, preferably with a relative path, directly from the root directory down
if (!is_file ($fp)) {//check if file exists
??? echo "";
??? Exit
} else {
??? $savefn = Iconv (' UTF-8 ', ' Gb2312//ignore ', $savefn);
??? $file = fopen ($fp, "R"); Open File
??? Header ("Content-type:application/octet-stream");
??? Header ("Accept-ranges:bytes");
??? Header ("Accept-length:". FileSize ($FP));
??? Header ("content-disposition:attachment; Filename= ". $SAVEFN); #下载时显示的文件名
??? Echo fread ($file, FileSize ($fp));
??? Fclose ($file);
??? Exit
}
?>