Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP basename( __DIR__ ) is returning _DIR_ on some servers

Tags:

php

filepath

dir

I'm hoping someone here knows the answer to this. I wrote a script that uses

basename( __DIR__ )

then uses an if file exist function.

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

Did this change with a version of PHP or is there some other setting that makes it so this doesn’t work?

Lastly is there a better way to get the path of the file? Here is the whole line I’m using:

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

(yes I know it's a WordPress function but this is not a WordPress question it's a PHP one)

like image 525
Brooke. Avatar asked Aug 14 '13 06:08

Brooke.


2 Answers

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

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

like image 50
Raptor Avatar answered Oct 06 '22 22:10

Raptor


If __DIR__ constant doesn't work on server A, while works on server B, then a PHP Version is an issue (As mentioned by @Shivan).

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

Here's a quick workaround for you:

// this should be at the top
if (!defined('__DIR__')) {
   define('__DIR__', dirname(__FILE__));
}
like image 41
Yang Avatar answered Oct 06 '22 21:10

Yang