start.php

<?php
if(file_exists('/config.php')) require_once('/config.php');
echo TEST;
?>

config.php

<?php
define('TEST','Hamsters');
?>

I have Windows XP + XAMPP with PHP Version 5.3.8.
If I run start.php it gives me this error:

Notice: Use of undefined constant TEST - assumed 'TEST' in C:\programs\xampp\htdocs\start.php on line 3

Now I modify start.php to the following and he gives me my Hamsters:

<?php
require_once('/config.php');
echo TEST;
?>

How can file_exists() say the file not exist but without the condition still be able to require_once() the file that claimed non-existent?

有帮助吗?

解决方案 3

The problem was that /config.php actually means C:/config.php.

File_exists() only checks the actual file or folder if it is exist and if not it will give a false.
Require_once() does some more. According to the PHP manual this function is almost identical to require() that is almost identical to include(). The non identical parts are not important in our case. What is important, that the manual says about include:

Files are included based on the file path given or, if none is given, the include_path specified. If the file isn't found in the include_path, include will finally check in the calling script's own directory and the current working directory before failing.

Because config.php was in the same directory where start.php was, after require_once() found out that config.php is not in C:/ it searched and found it in the directory where start.php called him. File_exists() does not do this search thus it returns false and require_once() can't be called.

If I copy my config.php file to C:/ it will work with the file_exists() way too.

其他提示

Making a require (or include) conditional is something you should not do at all:

if (file_exists('/config.php')) {
  require_once('/config.php');
}

Instead either take include if it's not a must, or take require if it's a must. See as well:

Wrapping include/require into conditions make those complicated and as includes are often related to program flow you really want to keep it simple.

Additionally some optimizations you might want to make use of later on are not possible with conditional includes.

and in your case, I wonder why you actually check for a file existence. Instead of require_once you most certainly meant include_once, the if is superfluous:

include_once('/config.php');

Here's what I think it is.

require_once tries its very best to find the file, and if it finds a file in the working directory or the calling script's directory, it will treat that directory as the root for the purpose of interpreting a leading slash.

file_exists is tighter: it looks for that absolute filesystem path and reports it not found.

a.php:

echo 'a';

main script:

require_once ('/a.php');
echo '<br/>',file_exists('/a.php') ? 'exists' : 'not';

yields:

a
not

to confirm:

require_once ('./a.php');
echo '<br/>',file_exists('./a.php') ? 'exists' : 'not';

yields:

a
exists
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top