Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$_SERVER['DOCUMENT_ROOT'] path not working

Tags:

path

php

I am using document root to provide absolute path which is not working. if i echo this path it turns out to be C:wamp/www/proman/header.php. I i give relative path it works fine what is the problem here?

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

I elaborate my problem here: I have 2 php files data_object.php and user.class.php. user.class.php has an include statement for data_object.php which is relative to user.class.php.These two files are under different directory hierarchy. Now I have to include this user.class.php in various files (like projects.php, links.php) under different hierarchy when I want to create a User() object. The problem is the relative path for file inclusion of data_object.php does work for say projects.php but if I open links.php the error message says it could not open file data_object.php in user.class.php. What I think is for relative inclusion of data_object.php it is considering the path of the file in which user.class.php is included. I am facing such problems in more than one scenarios I have to keep my directory structure the way it is but have to find a way to work with nested includes. I am currently running on a WAMP server but after completion I have to host the solution on a domain. Pls help

like image 432
Abhishek Avatar asked Dec 17 '22 00:12

Abhishek


1 Answers

Since that's a server variable, you may or may not see it, depending on which web server you're running under (especially IIS), or if something's configured weird.

One way to deal with this problem is to set the variable.

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

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

If you are actually trying to make a URL, then you just specify an absolute URL - /proman/header.php, or a relative URL - ../proman/header.php.

like image 157
Seth Avatar answered Dec 26 '22 16:12

Seth