If my URL is http://www.server.com/myapp/stuff/to/see
and my directory structure on disk is htdocs/myapp/*
, how can I extract the /myapp
part? Also, what if there was no application folder and the application root was just '/'? Is there a predefined variable for getting that value?
What I want is a function that is able to trim /myapp
off of the request URI, so that I'm only left with /stuff/to/see
. AND, if I were to move the application to the server document root, have it determine that as well (so that /stuff/to/see
is just returned as /stuff/to/see
)
My directory structure is this:
application |-config |-config.inc.php library |-autoload.class.php public |-css |-img index.php
So, from index.php, I want to know how to get what I asked above.
$pathInPieces = explode('/', $_SERVER['DOCUMENT_ROOT']); echo $pathInPieces[0]; This will output the server's root directory.
To simply access the app's root path, use the module as though it were a string: var appRoot = require('app-root-path'); var myModule = require(appRoot + '/lib/my-module. js'); Side note: the module actually returns an object, but that object implements the toString method, so you can use it as though it were a string.
The chroot() function changes the root directory of the current process to directory, and changes the current working directory to "/".
When you access a page using http://www.server.com/myapp/stuff/to/see
, and your webserver's document root is at /something/htdocs/
, then the current local
path is /something/htdocs/myapp/stuff/to/see
.
There is no definite solution to get /something/htdocs/myapp/
as a result now, because there is no clear definition which path you should get. When you have some rule for that, tell us, and we might be able to come up with something, but without computation, the only paths you can see are:
http://www.server.com/myapp/stuff/to/see
$_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']
/something/htdocs/myapp/stuff/to/see/index.php
$_SERVER['SCRIPT_FILENAME']
/something/htdocs/
$_SERVER['DOCUMENT_ROOT']
/myapp/stuff/to/see
$_SERVER['REQUEST_URI']
/myapp/stuff/to/see/index.php
$_SERVER['SCRIPT_NAME']
and $_SERVER['PHP_SELF']
For the web root, there is DOCUMENT_ROOT
as pointed out in several answers. For the application root, there is no way for the system to tell which folder in your application is the root folder. You will have to set that manually.
Most applications define an application root using dirname(__FILE__);
or a config setting and store that in a constant that is available throughout the application.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With