Home >Backend Development >PHP Tutorial >Solution to the problem that the file_exists function in PHP does not support Chinese names_PHP Tutorial
This article mainly introduces the solution to the problem that the file_exists function in PHP does not support Chinese names. It is a very practical skill that requires Friends can refer to it
Generally speaking, file_exists() is often used in PHP to determine whether a file or folder exists. If it exists, it returns true, otherwise it returns false. However, when the web page uses UTF8 encoding, this function cannot return the correct value for Chinese file names or folder names, and always returns false. After testing, we came up with a solution and analyzed that the reason for this situation should be that PHP cannot judge correctly due to different encodings.
The following code is code that cannot return the correct value. It returns that the file is not present regardless of whether it exists:
<?php; $file="/attachment/21/0/中文.rar"; $newfile = dirname(__FILE__).$file; echo file_exists($newfile); ?>
After testing, a sentence is added to convert UTF8 encoding to GB2312 encoding, and the correct judgment can be made:
<?php $file="/attachment/21/0/中文.rar"; $newfile = dirname(__FILE__).$file; $file=iconv('UTF-8','GB2312',$file); echo file_exists($newfile); ?>