Home > Backend Development > PHP Tutorial > 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中文网
Release: 2023-02-28 21:22:01
Original
1237 people have browsed it

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;]); 
?>
Copy after login
Copy after login

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;); 
}
Copy after login


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;]); 
?>
Copy after login
Copy after login

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)!

Related labels:
source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template