A. Lau A. Lau - 3 months ago 30
PHP Question

Way to get absolute path of file PHP

Is there a way to get the absolute path of a file while having it included in another script?

So I'm trying to include a file that's in folder A while I'm in folder B but it keeps trying to refer to folder B's location.

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


You can try like this

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

Since PHP 5.3.0 you can use also the magic constant __DIR__ More info in the docs

Another way is

$rootDir = realpath($_SERVER["DOCUMENT_ROOT"]);

include "$rootDir/yourfile.php";