$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Why File_exists returns true with or without ABSPATH?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Why File_exists returns true with or without ABSPATH?

matteradmin9PV0评论

I've been confused due to the result being true in both cases, with or without using ABSPATH constant for this:

if (file_exists(ABSPATH . 'wp-config.php')){
    echo "true";
}

or

if (file_exists('wp-config.php')){
    echo "true";
}

Please note that the file running this code is not in the root directory (it's in a subfolder inside a plugin folder in wp-content/plugins directory).

Any explanation would be helpful please. Also which one should I prefer to use.

I've been confused due to the result being true in both cases, with or without using ABSPATH constant for this:

if (file_exists(ABSPATH . 'wp-config.php')){
    echo "true";
}

or

if (file_exists('wp-config.php')){
    echo "true";
}

Please note that the file running this code is not in the root directory (it's in a subfolder inside a plugin folder in wp-content/plugins directory).

Any explanation would be helpful please. Also which one should I prefer to use.

Share Improve this question asked Dec 3, 2018 at 21:57 Nabeel KhanNabeel Khan 4185 silver badges17 bronze badges 2
  • 1 Is this file called with a direct request or is this a file included as plugin and request goes to index.php from WP root? – Krzysiek Dróżdż Commented Dec 3, 2018 at 22:25
  • 1 @KrzysiekDróżdż is on to it. The request is probably going to the index.php in the WP root and since wp-config.php is in the root, it will return a bool true. I think it's good practice to use ABSPATH for file_exists() all the time since the various wrappers supported by the function can result in some unexpected behavior. – jdp Commented Dec 3, 2018 at 22:30
Add a comment  | 

1 Answer 1

Reset to default 4

AFAIR, if you use file_exists with relative paths, you have to be very careful, because the path is relative to script that is called by request and not relative to file that contains that check...

So if it's a plugin and the request goes to main WP index.php, then you'll be looking for a file in WP root.

But if the same code will be run by AJAX call (wp-admin/admin-ajax.php) it will search for that file in wp-admin directory.

That's why you should use ABSPATH or use __DIR__ (to make the path relative to file in which you check the file existence).

Post a comment

comment list (0)

  1. No comments so far