PHP basename (__DIR__) returns _DIR_ on some servers

I hope someone here knows the answer to this question. I wrote a script that uses

basename( __DIR__ )

then uses the if file exists function.

On my server this works fine, however on another server it actually returns the word _DIR_ instead of the file path.

Has this changed with the version of PHP or is there some other parameter that makes it so that it does not work?

Finally, is there a better way to get the file path? Here is the entire Im line using:

define('NIFTY_CONSTANT', trailingslashit (WP_PLUGIN_DIR . '/'. basename( __DIR__ ) ). '/lib/mdetect.php' );

(yes, I know this is a WordPress feature, but this is not a WordPress question, this is PHP)

+4
source share
2 answers

__DIR__ introduced in PHP 5.3. Double check your version of PHP.

Link: http://php.net/manual/en/language.constants.predefined.php

+8
source

If the __DIR__ constant __DIR__ not work on server A , but works on server B , then the problem with PHP is the problem (as @Shivan mentioned).

You can simply test it by calling phpinfo() on both servers.

Here is a quick workaround for you:

 // this should be at the top if (!defined('__DIR__')) { define('__DIR__', dirname(__FILE__)); } 
+4
source

Source: https://habr.com/ru/post/1496962/


All Articles