In PHP, file_exists () is used to determine whether the Chinese file name is invalid,

Source: Internet
Author: User

In PHP, file_exists () is used to determine whether the Chinese file name is invalid,

This article describes how to use file_exists () in PHP to determine invalid Chinese file names. Share it with you 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. However, when file_exists is used, if your file name or path is Chinese, it is invalid in uft8 encoding documents. 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.
If the specified file or directory exists, true is returned; otherwise, false is returned.
Example 1
Copy codeThe Code is as follows: <? Php
Echo file_exists ("test.txt ");
?>
Output:
1
Example 2
Copy codeThe Code is as follows: Export realname+'chinese.txt ';

If (file_exists ($ realname )){
// Never get in here
}
Else
{
Echo 'www .jb51.net reminding you that the file does not exist ';
}
The output result is www.jb51.net, reminding you that the file does not exist.
However, I was surprised that the PHP file exists, and the PHP file is not in the same directory as the Chinese TXT file. So there is no problem in writing this file. When I think about it, will it be a Chinese problem? I will convert the encoding.

Solution:
Copy codeThe Code is as follows: Export realname+'chinese.txt ';
If (file_exists (iconv ('utf-8', 'gb2312', $ realname ))){
// This will support
}

The result shows 1. The problem is solved.
In addition, we also need to remind you that it is best not to use Chinese names in php, such as apache, linux, and php, which do not support Chinese very well, so you should try your best to use English.

I hope this article will help you with PHP programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.