Home  >  Article  >  Backend Development  >  Code to force download files in PHP (solve the problem of garbled Chinese file names under IE)_PHP Tutorial

Code to force download files in PHP (solve the problem of garbled Chinese file names under IE)_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:29:43860browse

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

Copy code The code is as follows:

$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']);
?>


There are two common ways to solve the Chinese garbled file name of PHP Header downloaded files in IE. One is to change the page encoding to utf8, and the other is to change the Chinese url Entering urlencode encoding can solve it.
Solution 1 (my page is utf-8 encoded):

Copy code The code is as follows:

$filename = "Chinese.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 . '"');
}


Solution 2

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

Copy code The code is as follows:
$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' ]);
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323364.htmlTechArticleA 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. Copy code...
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