-
- $file_name = urlencode($_REQUEST['filename']);
- header("Pragma: public"); header("Expires: 0");
- header("Cache-Control : must-revalidate, post-check=0, pre-check=0");
- header("Content-Type: application/force-download");
- header('Content-Type: application/vnd.ms-excel ; charset=utf-8');
- header("Content-Transfer-Encoding: binary");
- header('Content-Disposition: attachment; filename='.$file_name);
- echo stripslashes($_REQUEST['content ']);
- ?>
Copy the code
to solve the problem of garbled Chinese characters in the IE file name of PHP Header downloaded files:
One is to change the page encoding to utf8.
The other is to enter urlencode encoding for Chinese URLs.
Let’s look at the specific implementation code of the solution.
1. The page is utf-8 encoded:
-
- $filename = "中文.txt";
- $ua = $_SERVER["HTTP_USER_AGENT"];
- $encoded_filename = urlencode($filename);
- $encoded_filename = str_replace("+ ", "%20", $encoded_filename);
- header('Content-Type: application/octet-stream');
- if (preg_match("/MSIE/", $ua)) {
- header('Content-Disposition : attachment; filename="' . $encoded_filename . '"');
- } else if (preg_match("/Firefox/", $ua)) {
- header('Content-Disposition: attachment; filename*="utf8' '' . $filename . ''');
- } else {
- header('Content-Disposition: attachment; filename="' . $filename . ''');
- }
Copy code 2, Urlencode the file name first and then put it into the header.
Code:
- $file_name = urlencode($_REQUEST['filename']);
- header("Pragma: public"); header("Expires: 0");
- header("Cache- Control: must-revalidate, post-check=0, pre-check=0");
- header("Content-Type: application/force-download"); //Add forced download header information
- header('Content- Type: application/vnd.ms-excel; charset=utf-8');
- header("Content-Transfer-Encoding: binary");
- header('Content-Disposition: attachment; filename='.$file_name);
- echo stripslashes($_REQUEST['content']);
- ?>
Copy code
|