A way to get the absolute path to a PHP file

Is there a way to get the absolute path to a file when it is included in another script?

So, I'm trying to include a file that is in folder A when I am in folder B, but it continues to try to link to the location of folder B.

I tried dirname , $SERVER['PHP_SELF'] , cwd() , but they still return a relative path.

+6
source share
2 answers

You can try like this

 include dirname(__FILE__).'/../yourfile.php'; 

With PHP 5.3.0, you can also use the magic constant __DIR__ more information in the docs

Another way -

 $rootDir = realpath($_SERVER["DOCUMENT_ROOT"]); include "$rootDir/yourfile.php"; 
+12
source

You want __FILE__ , which is an absolute file path.

+7
source

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


All Articles