Search code examples
phpdocument-root

DOCUMENT_ROOT is not complete, missing domain folder


I have searched many threads so far but cant seem to find a solution. Inside one of my php scripts I am trying to get a server document root but the value I get is not complete, its simply missing the domain folder. I believe it is due to sharing hosting or smth else.

Here is the current way I am using:

$root = realpath($_SERVER["DOCUMENT_ROOT"]);

and the path I get is like:

/home/content/01/0151247/html

although I know it should be like:

/home/content/01/0151247/html/mydomain

I know as I compared it with SCRIPT_NAME and I see the mydomain there in the path.

Hope someone could direct me.

Thank you and sorry for probably asking another thousand time same question over community, I really tried things around from here, nothing helps me so far.

UPDATE

unfortunately I cant not simply use my index file with DIR as it is a wordpress setup and I am working on a separate folder where I am including some wordpress functionality but for that I need a document_root. If that would help.

UPDATE

apparantly the following way resolved my case, maybe it will help someone one day:

realpath($_SERVER["SUBDOMAIN_DOCUMENT_ROOT"]);

basically because of the server setup and domain configured as a subdomain.

Thanks to all who participated.


Solution

  • Prior to PHP 5.3 you can put a file in the directory whose path you want and define a constant:

    define('ROOT_DIR', dirname( __FILE__ ));
    

    After 5.3 you can just do:

    define('ROOT_DIR', __DIR__);
    

    The idea being that this would be in config.php of some sort that is included every time the application runs.

    Magic Constants Docs