About the encoding of a file that downloads Chinese names through the website __ Code

Source: Internet
Author: User

(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>";


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.