Let's take a look at the usage of file_exists.
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.
Syntax
File_exists (path) parameter description
Path is required. Specifies the path to be checked.
Example
<? Php
Echo file_exists ("test.txt ");
?>
Output:
1
Based on this principle, I started to work in the smarty template as follows:
Introduction
I found a problem and I will share it with you today. I will describe the entire process.
Problem
The company has a framework written based on smarty. I am responsible for php upgrade. After the maintenance personnel bring up the new environment, the tester asks me for frequent errors (errors: the file cannot be found ).
I tracked the code and reported an error in the place of smarty.
Error: The file does not exist.
Thinking process:
1. I thought this file was not found in the program. I found that the file exists. What is the problem?
2. Then I guess if there is a problem with the php kernel. I read the relevant php kernel file and I have not found any problems. As a result, I told the project manager about the entire process. -- The problem has not been solved, so the problem will not be solved first. The upgrade is delayed first.
Today I found this problem again. My first reaction was whether the php version was upgraded or not. -- Set the entire directory to 777 and it will be okay (chmod 777-R directory ). I want to see if it is a problem with the file_exists () method.
Therefore, I conducted relevant debugging and experiments.
Debug code:
Directory:
Observe the permissions: testhas no permissions for www. the execution file index.php has the read and write permissions, and the execution file test.txt has the read and write permissions.
Running result:
The execution permission of the directory affects file_exists ()
1. Grant test the maximum permission-755
If the operation succeeds, the file_exists () function is restricted by the Directory permission.
What directory permissions will affect file_exists?
I did a few experiments: 1. Any parent directory of the file. The file does not exist if you only have the write permission;
2. In any parent directory of the file, the file does not exist if the read permission is limited;
3. When all the parent directories have the execution permission, the system reports that the file exists and everything works normally.
The result indicates that when determining whether a file exists, file_exists () recursively determines whether each directory has the execution permission.
Change the file path to a relative path to get the same result.
Summary
As not mentioned in the php Manual, file_exists is affected by directory execution permissions. This article tells developers who have problems in this regard. This is the only rule.
Additional information
Thank you for your remarks. They are very useful. After your comments, I checked the manual again. Indeed, if the directory does not have the execution permission, the php-fpm application process will not be able to search for files under this directory, of course, the file does not exist (actually there is no executable directory ).
The content of the manual, which can be considered together, is mainly the execution permission. I will not discuss the read and write permissions in detail here (it is generally easy to understand ).
Clearly, the manual provides clear instructions on directory execution permissions. The execution permission on the directory is the right to be searched.
Finally, I found a Warning on the php official website.
If the file cannot be accessed due to security mode restrictions, the function returns FALSE. However, if the file is in the directory specified by safe_mode_include_dir, you can still use include to include the file. You can also pay attention to this.