Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of Chinese name garbled in php files

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to solve the problem of Chinese names and garbled codes in php documents, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Php document Chinese name garbled solution: 1, add charset code to the head of the php document: 2, set "iconv ('utf-8','gb2312'," upload/ ". $file [" name "]);" it's OK.

This article operating environment: Windows7 system, PHP7.1 version, Dell G3 computer.

The solution of garbled File name in php uploaded File

File upload is one of the most commonly used functions when dealing with form submission. We wrote a small demo today, as follows:

Let's first look at the structure:

Html is the page submitted by the form, php is the file that processes the form, and upload is the location where the file is uploaded

Html:

File upload

Php:

File = $_ FILES ["file"]; if ($file ["error"] > 0) {echo "error:". $file ["error"];} else {echo "file name:". $file ["name"]. "; echo" file type: ". $file ["type"]. File size: "; echo" ($file ["size"] / 1024). Location of temporary storage of "K"; echo "files:". $file ["tmp_name"]. ""; / / Save the uploaded file if (file_exists ("upload"). $file ["name"]) {echo $file ["name"]. "the file already exists";} else {/ / upload the file to move_uploaded_file ($file ['tmp_name'], "upload/" if the directory does not exist. $file ["name"]); echo 'file uploaded successfully';}}

I uploaded a .txt file as follows:

Execute:

You can see that it is going very well, so take a look at the results:

At this time, the uploaded file is what we want, but there is garbled, um, OK, let's solve it:

First, add a piece of code to the header of the php file:

Header ("Content-type: text/html; charset=utf-8")

Then define a variable:

$name = iconv ('utf-8','gb2312', "upload/". $file ["name"])

OK, let's take a look at the entire PHP page:

Header ("Content-type: text/html; charset=utf-8"); $file = $_ FILES ["file"]; if ($file ["error"] > 0) {echo "error:". $file ["error"];} else {$name = iconv ('utf-8','gb2312', "upload/". $file ["name"]); echo "file name:". $file ["name"]. "; echo" file type: ". $file [" type "]." Echo "File size:" ($file ["size"] / 1024). "K"; echo "temporary storage location of the file:". $file ["tmp_name"]. ""; / / Save the uploaded file if (file_exists ("upload". $file ["name"]) {echo $file ["name"]. " File already exists ";} else {/ / upload the file to if (move_uploaded_file ($file ['tmp_name'], $name)) {/ / move_uploaded_file ($file [' tmp_name']," upload/ ". $file [" name "]); echo 'file uploaded successfully!'; echo 'picture information:' Print_r ($file);}}

Run:

Perfect

Thank you for reading this article carefully. I hope the article "how to solve the problem of Chinese names and garbled codes in php documents" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report