Home >Backend Development >PHP Tutorial >The solution to the invalid determination of Chinese file names by file_exists() in PHP, _PHP tutorial

The solution to the invalid determination of Chinese file names by file_exists() in PHP, _PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:14:07877browse

The solution to the invalid determination of Chinese file names by file_exists() in PHP,

The example in this article describes the solution to the problem that file_exists() in PHP determines that Chinese file names are invalid. Share it with everyone for your reference. The specific method is as follows:

To determine whether a file exists in PHP, we will use the file_exists function or the is_file function, but when using file_exists, if your file name or path is in Chinese, it will be invalid when encoding the document in uft8. This article will solve this problem, let’s take a look below.

Definition and usage:
The file_exists() function checks whether a file or directory exists.
Returns true if the specified file or directory exists, false otherwise.
Example 1

Copy code The code is as follows:
echo file_exists("test.txt");
?>

Output:
1
Example 2
Copy code The code is as follows:
$realname='Chinese.txt';

if(file_exists($realname)) {
// You can never get in here
}
else
{
echo 'www.jb51.net reminds you that the file does not exist';
}


The output result is www.jb51.net to remind you that the file does not exist
But I was surprised that the file existed, and there was no problem with the path. The php file was in the same directory as the Chinese .txt, so there was no problem writing it this way. Then I thought about whether it might be a Chinese problem, and I converted the encoding

Solution:

Copy code The code is as follows:
$realname='Chinese.txt';
if(file_exists(iconv('UTF-8','GB2312',$realname))) {
// This will support it
}

The result shows 1, the problem is solved
In addition, I also need to remind everyone that it is best not to use Chinese names in PHP. Apache, Linux, and PHP do not support Chinese very well, so everyone should try to use English.

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/910601.htmlTechArticleThe solution to the invalid determination of file_exists() in PHP by file_exists(). This article describes the example of file_exists() in PHP. Solution to invalid Chinese file name. Share it with everyone for your reference. ...
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