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)

有帮助吗?

解决方案

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

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

其他提示

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__));
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top