Home > Article > Backend Development > Why does the php file_exists() function have no effect?
file_exists — Check whether the file or directory exists. If the specified file or directory exists, it returns TRUE, otherwise it returns FALSE. This article is a detailed analysis and introduction to the invalid solutions to file_exists in php. Friends in need can refer to it
Method 1: According to the official manual, if the safe mode related settings of php tutorial are too harsh, there will be a situation like this: even though the file actually exists, it will be mistakenly reported that the file does not exist.
Since we cannot control the php.ini on the server side, we cannot turn off the safe mode when using ini_set(). We can only settle for the next best thing and find a more reliable and safe detection method. to check if the file exists. We can achieve this with the help of $_server['document_root']. $_server['document_root'] returns the root directory of the website. The last subdirectory of the directory does not contain the directory identifier "/", such as:
d:/www/htdocs
With the root directory and the path of the file that needs to be detected, we will get an absolute path, and PHP can successfully use the file_exists() function to detect it. We only need to change the first line of the above code (note that we added the symbol "/" before config.php):
$file=$_server['document_root']."/config.php" ;
In this way, the execution of the code is very reliable and there will be no unexpected results.
The above method is also applicable to the related detection function of directory (is_dir()) or file (is_file()), which can detect whether the directory or file that is protected by security exists.
Finally, by the way: This type of file protected by PHP special settings does not need to be added with $_server['document_root' when referencing (include and require) ] paths because, according to the PHP documentation, they are allowed to be quoted.
Method 2: In my case, the file was moved back and forth between windows and linux. As a result, the access permissions of files and directories under Linux are changed, resulting in no one except the owner of the file having access permissions. Using chmod -r 755 xxx/* solved the problem.
The above is the detailed content of Why does the php file_exists() function have no effect?. For more information, please follow other related articles on the PHP Chinese website!