PHP file_exists () to determine the Chinese file name invalid solution _php Tips

Source: Internet
Author: User

This article illustrates the solution of file_exists () in PHP to determine the Chinese filename is invalid. Share to everyone for your reference. The specific methods are as follows:

PHP to determine whether the existence of the file we will use the File_exists function or Is_file function, but when using file_exists if your file name or path is Chinese in the UFT8 encoding document is invalid. This article is to solve this problem, let's take a look at the following.

Definition and Usage:
the file_exists () function checks whether a file or directory exists.
Returns true if the specified file or directory exists, or FALSE.
Example 1

Copy Code code as follows:
<?php
Echo file_exists ("test.txt");
?>

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

if (file_exists ($realname)) {
Never going to get in here.
}
Else
{
Echo ' Www.jb51.net reminds you that the file does not exist ';
}


The output is www.jb51.net to remind you that the file doesn't exist.
But I'm surprised. File is there, and the path is no problem php file with Chinese. txt in the same directory so write is no problem, think about it is not a Chinese problem, I have to convert the code

Solution:

Copy Code code as follows:
$realname = ' Chinese. txt ';
if (File_exists (Iconv (' UTF-8 ', ' GB2312 ', $realname))) {
So that we can support the
}

The results show 1, the problem is solved
In addition, we also need to remind you that in PHP, it is best not to use Chinese names, such as apache,linux,php of Chinese support is not good, so we try to use English.

I hope this article will help you with your PHP program design.

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.