Resolve the invalid solution for php file_exists

  • 2020-06-23 00:01:07
  • OfStack

Method 1: According to the official manual, if the safe mode Settings for the php tutorial are too harsh, a situation occurs where a file is mistakenly reported as not being there, even though it is real.

Since we couldn't manipulate the server php.ini and couldn't turn off the safe mode mode with ini_set(), we had to settle for a more reliable and secure detection method to detect the existence of the file. We can do this with $_server['document_root']. $_server['document_root'] returns the site's root directory, the last subdirectory of which does not contain the directory flag "/", as follows:
d:/www/htdocs
With the root directory and the path of the file to be detected, we get an absolute path that php can detect using the file_exists() function. We only need to change the first behavior in 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 trusted without unexpected results.
The above method also applies to the relevant detection function of directory (is_dir()) or file (is_file()), which can detect the existence of the directory or file being protected.

Finally, by the way, this type of file protected by php's special Settings does not have to include the $_server['document_root'] path when referring to (include and require) because, according to the php documentation, they are allowed.

Method 2: My case is due to the file moving back and forth between windows and linux. Causes the access rights of files and directories under linux to be changed, resulting in no access to the file except the owner. The problem was solved using ES47en-ES48en 755 xxx/*.

Related articles: