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

怪我咯
Release: 2023-03-13 13:50:01
Original
1451 people have browsed it

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

 file_exists ( $filename )
Copy after login

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);
?>
Copy after login

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);
?>
Copy after login

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!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!