Path $ _SERVER ['DOCUMENT_ROOT'] does not work

I use the root of the document to provide an absolute path that doesn't work. if I repeat this path it will be C: wamp / www / proman / header.php. I give a relative path, it works fine, what's the problem?

$path = $_SERVER['DOCUMENT_ROOT']."proman/header.php";

I detailed my problem: I have 2 php files data_object.php and user.class.php. user.class.php has an include statement for data_object.php, which refers to user.class.php. These two files are in different directory hierarchies. Now I have to include this user.class.php in various files (e.g. projects.php, links.php) under a different hierarchy when I want to create a User () object. The problem is that the relative path to include the data_object.php file really works for say projects.php, but if I open links.php, the error message says that it cannot open the data_object.php file in user.class.php. I think that for the relative inclusion of data_object.php it considers the path to the file that user.class.php is included in. I encountered such problems in more than one scenario,I need to keep the directory structure as it is, but I need to find a way to work with nested inclusions. I am currently working on a WAMP server, but after completion I have to host the solution in the domain. Pls Help

+3
source share
2 answers

With this server variable, you may or may not see it, depending on which web server you are using (especially IIS), or if something is configured weird.

One way to solve this problem is to set a variable.

$_SERVER['DOCUMENT_ROOT'] = "C:/some/absolute/path";
// or, if you put this code in a file that in your document root: 
$_SERVER['DOCUMENT_ROOT'] = dirname(__FILE__);

Then you can either require()this file when you need to check the document root, or use the auto_prepend_file php.ini parameter to include the file automatically.

If you are actually trying to create a URL, you simply provide an absolute URL - /proman/header.phpor relative URL - ../proman/header.php.

+6
source

In my experience, $_SERVER['DOCUMENT_ROOT']does not include the trailing slash. Try:

$path = $_SERVER['DOCUMENT_ROOT'].'/proman/header.php';
+5

Source: https://habr.com/ru/post/1749457/


All Articles