(In the spirit of the Chinese nation as an open-minded principle, the first note that this article is the author himself after several twists and turns to get summary, not all letter)
Problem Description:
Through PHP upload the Chinese name of the file, uploaded after the download of the Chinese name of the file when there is a garbled problem.
reason for the occurrence:
Upload files through PHP, and did not use Utf-8 encoding method upload, but on the Web page through the <a> tag, when the browser search for Chinese file names by Utf-8 encoding method to find.
(The site's learning authors are also a little, about the site server, client and other mechanisms do not have enough knowledge)
the right approach:
When uploading a file via PHP, the path of the file is set to the Utf-8 encoding format, and the file path is converted to UTF-8 encoding when it is downloaded.
upload code correctly:
<form action= "importmonthexcel.php" method= "Post" id= "Form1" enctype= "Multipart/form-data" >
<?php echo "<input id= ' chimproj ' name= ' ChoseFile1 ' type=file value=init/>"; echo "<input type= ' submit ' value= ' Determine '/> '; The following code is just to make the $_post channel not empty echo "<input id= ' Submitjudge ' name= ' fasdf ' value= ' Fasdfasdfasdfasdfas ' type=hidden/> '; ?> </form> <?php You need to upload the file is selected, do the following if ($_post) { Header ("content-type:text/html; charset=gb2312 "); Date_default_timezone_set ("asia/chongqing"); Must be an. xls or. xlsx file, and must be less than 5Mb if ($_files["ChoseFile1" ["type"] = = \ "Application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" \ || $_files["ChoseFile1" ["type"] = = "application/vnd.ms-excel") \ && $_files["File" ["Size"] < 5242880) { Move_uploaded_file ($_files["ChoseFile1" ["tmp_name"], \ The path settings saved after uploading become UTF-8 encoded Iconv ("gb2312", "UTF-8", "uploadfile/". $_files["ChoseFile1" [' Name '])); echo "<script>alert (\" import success \ ");</script>"; Auto Close window echo "<script>window.opener=null; window.open (', ' _self '); Window.close (); </script> ";
}else{ echo "Import Failed"; } } ?> |
Download the code correctly:
$filepath is the path to the Chinese name file Convert the Utf-8 path to the encoding format of the Web page when downloading $filepath = Iconv ("UTF-8", "gb2312", $filepath); echo "<a href=\" $filepath \ "> Downloads </a>"; |