Home  >  Article  >  Backend Development  >  Detailed examples of solutions to the problem that the file_exists function in PHP does not support Chinese names

Detailed examples of solutions to the problem that the file_exists function in PHP does not support Chinese names

怪我咯
怪我咯Original
2017-07-11 10:37:191412browse

file_exists - Check whether the file or directory exists; the syntax is as follows

 file_exists ( $filename )

The parameter filename is the path of the file or directory. Returns TRUE if the file or directory specified by filename exists, FALSE otherwise.

But this function cannot return the correct value for Chinese file names or folder names when the web page uses UTF8 encoding, 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 cannot return the correct value, regardless of whether the file is present or not:

<?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. , you can make a correct judgment:

<?php
$file="/attachment/21/0/中文.rar";
$newfile = dirname(FILE).$file;

$file=iconv(&#39;UTF-8&#39;,&#39;GB2312&#39;,$file);

echo file_exists($newfile);
?>

The above is the detailed content of Detailed examples of solutions to the problem that the file_exists function in PHP does not support Chinese names. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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