php - get $_SERVER["DOCUMENT_ROOT"] without using superglobals -
i'm running php script command line, superglobals not defined. how can $_server["document_root"] without using superglobal $_server?
superglobals such $_server
haven't been set yet (because apache/nginx/etc) sets these values. you're running command line, there no apache/nginx/etc set them, resulting in them becoming null
.
there 2 alternatives can use.
using __dir__
you can use magic constant __dir__
output directory of current file.
php -r "echo __dir__;" /var/www/html
this give different results depending on run from. i.e: /var/www/html/lib/foo
return differently if ran /var/www/html/views/index
. - not you're looking for.
setting own constant
within bootstrap file (as mentioned in comment), can set own constant used throughout application.
define('__document_root__', __dir__);
now within page run (as long uses bootstrap file), can call __document_root__
constant value would mirror $_server["document_root"]
. though require use relative paths include bootstrap file.
Comments
Post a Comment