Home > Article > Backend Development > Solve the problem that file_get_contents cannot open the Chinese file name_PHP tutorial
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.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633181.html