Solve the problem that file_get_contents cannot open the Chinese file name_PHP tutorial

WBOY
Release: 2016-07-13 10:43:33
Original
1111 people have browsed it

Use file_get_contents to open a file or collect a remote server file. If Chinese characters are encountered in the file name or URL, the failed to open stream: Lnvalid argument in error will appear.

I encountered a very difficult problem today. The customer used ftp to transfer a file to the server, but it could not be recognized by the program. After checking the code, no problem was found. Finally, after replaying the process, I found that the file uploaded by the customer was named in Chinese. .So I did a little test and found that the file_get_contents function does not support Chinese file names.

The original code is as follows:

                                                                                                                        echo file_get_contents('./'.$filename);

The result will be displayed after execution:

The tragedy begins.

After that, I tried a lot of judgment methods and found out whether it was an encoding problem. I checked the file encoding and code encoding over and over again, but they all matched very well.

I checked the relevant information but couldn’t find it, so I googled and found that someone had encountered this problem, saying it was a system encoding problem, so I started transcoding the file name.

The file is encoded in utf-8, but the system defaults to gbk. So convert the file name to gbk first and then read it.

                                                                                                                       

$filename=iconv('utf-8','gb2312',$filename);

> // echo file_get_contents (mb_convert_encoding ('./ha .txt', 'gbk', 'utf-8')); (another method)

echo file_get_contents('./'.$filename);

Test results:


Read successfully.

OK, you’re done.

http://www.bkjia.com/PHPjc/633181.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633181.html

TechArticleUse file_get_contents to open files or collect remote server files. If Chinese characters are encountered in the text name or URL, failed to will appear. open stream:Lnvalid argument in error. Today I met someone...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template