How can I find the directory path on my server. I need this to include scripts from the root directory AND from my includes folder which is on the root directory.
Both include fine when trying to include a script from the root directory, but how would I include a script that is in my includes folder from a folder inside a folder.
You cannot include http://www.mysite.com/includes/config.php from http://www.mysite.com/folder1/folder2/index.php. You can include /usr/www/sites/mysite.com/includes/config.php from /usr/www/sites/mysite.com/folder1/folder2/index.php. That is the first. Now you should obtain the full absolute path to your document root which in this case will be /usr/www/sites/mysite.com, and resolve your relative paths from that absolute path:
PHP Code:
include $_SERVER['DOCUMENT_ROOT'].'/includes/config.php';
To avoid problems with refering to local files by relative paths you should always refer to them by absolute path. It is not necessary to have a $_SERVER['DOCUMENT_ROOT'] as root folder, you can define some other variable in a script which is included by each and every script, e.g. common.php, which would contain something like following:
PHP Code:
if (!isset($_SERVER['DOCUMENT_ROOT']))
$_SERVER['DOCUMENT_ROOT'] = dirname(__FILE__);
Obviously $_SERVER['DOCUMENT_ROOT'] can be not set if you run your script from command line, because it is apache variable passed to php as module.
Problem of relative paths is the most popular one on this forum for the last month. And I guess it will remain the most popular forever. If you could read in Russian I'd recommend one very good article about php which clears out how php works, what php can and what it can not. But I definitely won't translate it.