PHP file_exists Check whether a file or directory exists _php tutorial

Source: Internet
Author: User
Tags php website
It is no problem to use file_exists to check whether a file or directory exists under normal circumstances, but be aware that one is Safe mode and the directory does not have execute permission, it will return false oh.

Let's take a look at the usage of file_exists.

The file_exists () function checks whether a file or directory exists.

Returns true if the specified file or directory exists, otherwise false.

Grammar
File_exists (path) parameter description
Path is required. Specifies the path to check.
Example
Echo file_exists ("test.txt");
?>

Output:

1
According to this principle I started working on the Smarty template as follows

Introduced

I have found a problem to share with you today. I'll describe the whole process.

Problem

The company has a framework is based on Smarty write, I am responsible for the upgrade of PHP, maintenance personnel to the new environment after the test staff to ask me to make frequent errors (error: Prompted to find the file).
I traced the code, which was the mistake of the Smarty's local newspaper.


Error: The file reported here does not exist.

Thinking Process:

1, I thought that the program does not have this file, the results found that the document is there, that is how it?

2, later I guess is not the PHP kernel has a problem, I took the relevant kernel file PHP read it again, did not find the problem. As a result, I told the project manager the whole process. --It didn't work out, so let it go. The escalation of the matter was delayed first.

Today I found this problem, my first reaction is that the PHP version is not upgraded, found no. --and then set the entire directory to 777 is OK (chmod 777-r directory). I think it is not the file_exists () method problem.

So, I have done the relevant debugging and experiment.

Debug code:

Directory:

Observe each permission: Test does not have any permission to WWW, while execute file index.php have read and write Execute permission, include file Test.txt have read and write permission.

Operation Result:

Execution permissions on the directory affect file_exists ()

1. Give test a maximum permission--755

Now that it succeeds, the file_exists () function is limited by the permissions of the directory.

What kind of directory permissions affect File_exists ()?

I did a few experiments: 1, the file of any parent directory, only write permission times file does not exist;

2, the file of any superior directory, only read the permission is also reported that the file does not exist;

3, and when all the parent directory has execute permission, the paper is present, everything is normal.

As a result, file_exists () is recursive to determine if a file exists, and whether it has execute permissions for each directory.

Modify the file path to a relative path and produce the same result.

Summarize

It is not mentioned in the PHP manual that file_exists is affected by the execution permissions of the directory. This article tells the developers who have this problem. Just take this as a ring.

Additional Information

Thank you for your stay, very useful. After everybody's opinion I checked the manual again, indeed, if the directory does not have the execution permission, the PHP-FPM application process cannot go to this directory to carry on the file search, certainly thought that the file does not exist (actually thinks that the executable directory does not exist).
Manual content, you can consider together, mainly the implementation of permissions. In this case, the reading and writing permissions are not discussed (generally well understood).

Clearly, there is a clear indication of the permission to execute the directory. Execute permissions on the directory are the right to be searched.

Finally, I found a sentence on the PHP website warning
The function returns FALSE if the file cannot be accessed because of security mode restrictions. However, if the file is within the directory specified by Safe_mode_include_dir, it can still be included using the include. We can also pay attention to this.

http://www.bkjia.com/PHPjc/632216.html www.bkjia.com true http://www.bkjia.com/PHPjc/632216.html techarticle It is no problem to use file_exists to check whether a file or directory exists under normal circumstances, but be aware that one is Safe mode and the directory does not have permission to execute when it returns F ...

  • 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.