Home  >  Article  >  Backend Development  >  Win7 displays the file suffix name. Code to force file download in PHP (solve the problem of garbled Chinese file names under IE)

Win7 displays the file suffix name. Code to force file download in PHP (solve the problem of garbled Chinese file names under IE)

PHP中文网
PHP中文网Original
2017-04-01 16:33:301186browse

A problem encountered in the middle is that the Chinese file name submitted directly into the header will become garbled under IE. The solution is to urlencode the file name first and then put it into the header, as follows.

The code is as follows:

<?php 
$file_name = urlencode($_REQUEST[&#39;filename&#39;]); 
header("Pragma: public"); header("Expires: 0"); 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
header("Content-Type: application/force-download"); 
header(&#39;Content-Type: application/vnd.ms-excel; charset=utf-8&#39;); 
header("Content-Transfer-Encoding: binary"); 
header(&#39;Content-Disposition: attachment; filename=&#39;.$file_name); 
echo stripslashes($_REQUEST[&#39;content&#39;]); 
?>

There are two common ways to solve the problem of Chinese garbled characters in the IE file name of PHP Header downloaded files. One is to change the page encoding to utf8, and the other is to enter urlencode encoding for the Chinese URL. .
Solution one (my page is utf-8 encoded):

The code is as follows:

$filename = "中文.txt"; 
$ua = $_SERVER["HTTP_USER_AGENT"]; 
$encoded_filename = urlencode($filename); 
$encoded_filename = str_replace("+", "%20", $encoded_filename); 
header(&#39;Content-Type: application/octet-stream&#39;); 
if (preg_match("/MSIE/", $ua)) { 
header(&#39;Content-Disposition: attachment; filename="&#39; . $encoded_filename . &#39;"&#39;); 
} else if (preg_match("/Firefox/", $ua)) { 
header(&#39;Content-Disposition: attachment; filename*="utf8&#39;&#39;&#39; . $filename . &#39;"&#39;); 
} else { 
header(&#39;Content-Disposition: attachment; filename="&#39; . $filename . &#39;"&#39;); 
}


Solution two
Urlencode the file name first and then put it into the header, as follows.
The code is as follows:

The code is as follows:

<?php 
$file_name = urlencode($_REQUEST[&#39;filename&#39;]); 
header("Pragma: public"); header("Expires: 0"); 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
header("Content-Type: application/force-download"); 
header(&#39;Content-Type: application/vnd.ms-excel; charset=utf-8&#39;); 
header("Content-Transfer-Encoding: binary"); 
header(&#39;Content-Disposition: attachment; filename=&#39;.$file_name); 
echo stripslashes($_REQUEST[&#39;content&#39;]); 
?>

The above is the code for forced downloading of files in win7 display file suffix name in php (solve the problem of garbled Chinese file names under IE). For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn