Solution of file_exists of in PHP to Judge Invalid Chinese Filename

  • 2021-08-03 09:27:17
  • OfStack

This paper gives an example of how file_exists () in PHP judges that the Chinese file name is invalid. Share it for your reference. The specific methods are as follows:

In php, we will use file_exists function or is_file function to judge whether the file exists, but when using file_exists, if your file name or path is Chinese, it is invalid when encoding the document in uft8. This article will solve this problem, let's take a look at it.

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

<?php
echo file_exists("test.txt");
?>

Output:
1
Example 2
$realname=' Chinese .txt';
if(file_exists($realname)) {
   // You'll never get in here
}
else
{
  echo 'www.ofstack.com Remind you that the file no longer exists ';
}

The output is www. ofstack. com reminding you that the file no longer exists
But I was surprised that the file exists, and the path is no problem. php file and Chinese. txt in the same directory, so it is no problem to write this way. When I think about whether it will be a Chinese problem, I will convert the code

Solution:

$realname=' Chinese .txt';
if(file_exists(iconv('UTF-8','GB2312',$realname))) {
   // So you can support it
}

The results show that 1, the problem is solved
In addition, we need to remind everyone that it is best not to use Chinese names in php, such as apache, linux and php, which are not very good at supporting Chinese, so try to use English.

I hope this article is helpful to everyone's PHP programming.


Related articles: