I have always used relative paths in PHP. Last time I have realized that there were something like absolute paths.
But what is the best way to use them?
getenv("DOCUMENT_ROOT"); ?
dirname(__FILE__); ?
How do you deal with the implementation? Please give some ideas.
When I want to use files that are “relative” to the one in which I am writing some code, I’m always using :
This allows me to :
For instance, to include a file that’s in a “classes” sub-diretory, I would generally use :
Note that this will work even if you’re executing your script from the command-line — while a solution based on some kind of DocumentRoot will probably not work when not using a web-server.
And, with PHP >= 5.3, you can also use the
__DIR__magic constant, which has exactly the same value asdirname(__FILE__), but is evaluated at compile-time, and not execution-time — which is a bit better for performances.