phpdocument-root

Php get webfolder address


I would like to know what PHP function/keyword I can use so that I can always get to the root of my webfolder. I'm using apache server with xampp.

I have a web folder by the name 'test1', so it's in the following path: D:\xampp\htdocs\test1

Currently I'm using $_SERVRER['Document_Root'] to get the above information. I fear if I proceed with uploading this to an actual webserver the site might not work.

Is there a way where I can only get test1 so that when I create links, I don't need to worry about Document_Root


Solution

  • So you really want to get the base url which points to D:\xampp\htdocs\test1 or whatever subdirectory under D:\xampp\htdocs\ you wanna call it.

    Now, i assume that in test1 directory there is an index.php file. If so, define a variable $base_url = dirname($_SERVER['PHP_SELF']);, it will return the full URL to your index.php file. You also might want to append a trailing slash to your $base_url

    // Note

    Maybe i misunderstood your requirements, i gave you a way to get the base url pointing to your index.php file under whatever sub-directory in D:\xampp\htdocs\. Now, talking about base directory, which is different, i would go around with something like:

    $base_dir = dirname(__FILE__);
    

    This variable would be defined into the index.php itself. Again, if you want to add a backslash/trailing slash (depending on the server environment). Append it like this:

    $base_dir .= DIRECTORY_SEPARATOR;