Is "realpath" redundant in $ absolutePath = realpath (__ DIR__)? Is __DIR__ always a canonical absolute path?

Is it possible to do something like

$dir = realpath(dirname(__FILE__)); // or for php 5.3+: $dir = realpath(__DIR__) 

Or are magic constants like DIR and FILE always return an absolute path, so is the realpath redundant?

+7
source share
1 answer

You do not need realpath() .

__FILE__ Documentation:

Full path and file name. If used inside include, the name of the included file will be returned. Starting with PHP 4.0.2, __FILE__ always contains an absolute path with the removal of symbolic links, whereas in older versions it contained a relative path in some circumstances.

So __FILE__ returns the absolute path => realpath() unnecessary

And if you use __DIR__ :

File directory. If used inside include, then the directory returns the returned file. This is equivalent to directory_name ( __FILE__ ) . This directory name does not have a trailing slash if it is not the root directory. (Added in PHP 5.3.0.)

__DIR__ also returns an absolute path, because dirname() does not disable the absolute part of the path __FILE__ => realpath() unnecessary

+8
source

All Articles