문제

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.

도움이 되었습니까?

해결책

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

다른 팁

UPDATE

In the config file, you can just append the DOCUMENT_ROOT variable:

$_SERVER['DOCUMENT_ROOT'] = $_SERVER['DOCUMENT_ROOT'] . '/mydomain';

And that should take care of it for you.

Old Solution

The DOCUMENT_ROOT is an environment variable set by the server. So if this is on shared hosting, you cannot changed. An alternative is to set your own constant to it, so in a config type file that is included on your pages you can do something like:

define('DOCUMENT_ROOT', $_SERVER['DOCUMENT_ROOT'] . '/mydomain');

And then just use that constant in place of $_SERVER['DOCUMENT_ROOT']. The other option is to contact your host and inquire about it, maybe it was an oversight on their part and they will fix it.

EDIT

Probably using the __DIR__ as others have posted about is the better way, as the DOCUMENT_ROOT can be set to different items and at least with the __DIR__ you should get an accurate directory each time.

Personally, to get the root of a folder in PHP, I use this in the my index file:

define('ROOT', dirname(__FILE__));     // __DIR__ will work under PHP 5.3
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top