TP problem: use TP to traverse all the file names of a directory in the Public folder. after testing, the Chinese file names are garbled,
Files are all UTF-8 encoded. This problem does not occur during Native PHP testing. Special advice.
The following is the code in TP:
Public function index () {$ dir = ". /Public/Articles "; // document directory $ data = $ this-> dirList ($ dir); $ arr = array (); foreach ($ data as $ v) {$ arr [] = $ v;} print_r ($ arr); // Chinese garbled characters $ this-> ajaxReturn ($ arr);} public function dirList ($ dir) {$ arr = scandir ($ dir); foreach ($ arr as $ val) {if ($ val! = '.' & $ Val! = '..') {$ Arr2 [] = $ val ;}} return $ arr2 ;}
Reply to discussion (solution)
The Chinese file names in the window system are all gbk encoded.
Do you think it is a small problem? None!
If the environment on which it depends is not very good, it will be strange if there are no major problems.
Do you think it is a small problem? None!
If the environment on which it depends is not very good, it will be strange if there are no major problems.
Even if I change the file to GBK encoding, the same result is returned. Native PHP is normal and will not be garbled. So I don't know what's going on.
In addition, PHP opened a Chinese text and needed to encode it, which is too earthy.
What if I set the header before ajaxReturn?
Header ('content-type: application/json; charset = utf-8 ');
What if I set the header before ajaxReturn?
Header ('content-type: application/json; charset = utf-8 ');
No. I added
Header ('content-type: text/html; charset = gbk ');
However, Chinese characters can be printed, but the data returned by ajax is NULL, which is depressing.
What if I set the header before ajaxReturn?
Header ('content-type: application/json; charset = utf-8 ');
No. I added
Header ('content-type: text/html; charset = gbk ');
However, Chinese characters can be printed, but the data returned by ajax is NULL, which is depressing.
Header ('content-type: tex/html; charset = gbk ');
The ajaxReturn function outputs data in json format by default. if the header is declared as text/html, the return value must be null. if it is changed to application/json, try again.
1. the default character set in linux is UTF-8.
2. the default character set for simplified Chinese windows is gbk.
3. the TP operating character set is UTF-8.
So TP does not have problems in linux
But there will be garbled characters in windows.
Therefore, after reading the file name, you must convert the character set according to the operating system.
Php json Functions only support the UTF-8 character set
PHP opens a Chinese text and also needs to encode it. it is too earthy.
It's just that you're asking yourself for trouble.
1. the default character set in linux is UTF-8.
2. the default character set for simplified Chinese windows is gbk.
3. the TP operating character set is UTF-8.
So TP does not have problems in linux
But there will be garbled characters in windows.
Therefore, after reading the file name, you must convert the character set according to the operating system.
Php json Functions only support the UTF-8 character set
PHP opens a Chinese text and also needs to encode it. it is too earthy.
It's just that you're asking yourself for trouble.
My text is garbled whether it is GBK encoding or UTF-8. The text name accepted by ajax is NULL.
PHP opens the text of the Chinese name. if it is not converted into a code, it is garbled. Is there any good way?
Displaying data of different codes in the same environment always requires garbled characters.
For the file name in windows, you must execute iconv ('gbk', 'utf-8', $ name); to utf8 encode the output.
To read a Chinese file name in windows, convert it to encoding.
The best way is to change to linux, such as installing a linux virtual machine on windows.
Otherwise, it will block a function for reading a file. the function is used for reading a file. write and convert the function.
For the file name in windows, you must execute iconv ('gbk', 'utf-8', $ name); to utf8 encode the output.
Is the path converted with Chinese characters? It seems garbled.
$ Data = $ this-> dirList ($ dir );
Output data must be converted to GBK for normal display. Then the return value in ajax is NULL.
To read a Chinese file name in windows, convert it to encoding.
The best way is to change to linux, such as installing a linux virtual machine on windows.
Otherwise, it will block a function for reading a file. the function is used for reading a file. write and convert the function.
The code conversion fails. can you give me some advice?
$ Data = $ this-> dirList ($ dir); here
Header ('content-type: text/html; charset = gbk') must be added to print data ');
In order to display normally in the browser, UTF8 does not work. Then the return value in ajax is NULL.
Gbk-> utf8
$ File = mb_convert_encoding ($ file, 'utf-8', 'utf-8, GBK, ASCII ');
Utf8-> gbk
$ File = iconv ('utf-8', 'gbk // IGNORE ', $ file );
Gbk-> utf8
$ File = mb_convert_encoding ($ file, 'utf-8', 'utf-8, GBK, ASCII ');
Utf8-> gbk
$ File = iconv ('utf-8', 'gbk // IGNORE ', $ file );
Thank you. I read a blog last night and got it done. during traversal, I had to convert and encode each element and then store it into an array.