Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP $_SERVER['DOCUMENT_ROOT'] vs realpath(dirname(_FILE_))

Tags:

php

I was wondering if one method was better than the other. I have been using the $_SERVER method to point to the current directory but is this method better than the realpath(dirname(_FILE_)) method or is there any difference?

like image 209
technoY2K Avatar asked May 25 '14 09:05

technoY2K


1 Answers

The $_SERVER[ 'DOCUMENT_ROOT' ] variable returns a server setting. Specifically it returns:

The document root directory under which the current script is executing, as defined in the server's configuration file.

The realpath( dirname( __FILE__ ) ) function will return the path of the folder that the current script is actually residing.

One is returning the value of a server setting and the other evaluates to a file's path. In some cases, the values will be the same but you should be aware of the difference between the two.

like image 109
Lix Avatar answered Nov 16 '22 22:11

Lix