cars
cars

Reputation: 441

Where to set PHP $_SERVER['DOCUMENT_ROOT'] Trailing Slash?

Sometimes $_SERVER['DOCUMENT_ROOT'] returns with a trailing slash. In other environments it does not. Where can this be specified?

Upvotes: 7

Views: 7558

Answers (7)

gohdan
gohdan

Reputation: 41

When using virtual hosts, Apache writes into $_SERVER['DOCUMENT_ROOT'] the value of DocumentRoot of virtual host. So you can write trailing slash there.

But it's not good decision because different hosters treat this paramenter differently so your application should not rely on it and must determine the presence of trailng slash without assistance.

Upvotes: 0

KingMo
KingMo

Reputation: 1

Old question, I know, but since it kind of gave me an idea how to solve this for myself I'll just add my solution here. I wanted to define a constant (ROOTPATH) with the $_SERVER['DOCUMENT_ROOT'] and ensure it has a trailing slash (DIRECTORY_SEPARATOR).

define('ROOTPATH', (ctype_alnum(substr($_SERVER['DOCUMENT_ROOT'], -1)) ? $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR : $_SERVER['DOCUMENT_ROOT']));

This assumes the last char of the folder name is alphanumeric (I couldn't remember seeing many folder names which end with special chars). If you want to make sure there isn't a trailing slash you could go with something like this:

define('ROOTPATH', (!ctype_alnum(substr($_SERVER['DOCUMENT_ROOT'], -1)) ? substr($_SERVER['DOCUMENT_ROOT'], 0, -1) : $_SERVER['DOCUMENT_ROOT']));

Peace, Mo

Upvotes: 0

skyrail
skyrail

Reputation: 149

Hakre answer is the right one. I tried to use include in different situations, in a console script or on a web server. My best bet was to use an absolute path, starting with DOCUMENT_ROOT. But I was still stuck because of that trailing slash. Here is what seems to be a good solution:

include_once getenv ("DOCUMENT_ROOT")." ./ WEB-INF/classes

Then invoke your script with php test.php from a batch script, and put a value to DOCUMENT_ROOT env variable or not, with a trailing slash or not. When loaded from apache, getenv is already filled.

The php engine can manage with ././, telling it the same as ./. In the same way htdocs./ is interpreted has htdocs/ (under windows)

Upvotes: 0

Sergey Onishchenko
Sergey Onishchenko

Reputation: 7861

You can do like this, in order to make sure the trailing slash is always present

'/'.trim( $_SERVER['DOCUMENT_ROOT'], '/' ).'/'

require_once( '/'.trim( $_SERVER['DOCUMENT_ROOT'], '/' ).'/'.'constants.php' );

Upvotes: 3

MatCarey
MatCarey

Reputation: 2439

I tend to use the current directory more than I use docroot because it also works well on command line and in unit tests too. I tend to use something like:

require_once(dirname(__FILE__).'/../../../../constants.php');

Rather than:

require_once($_SERVER['DOCUMENT_ROOT'].'/../constants.php');

I saw it first in Wordpress source and quite liked it, but it can lead to a lot of '../' repetition.

P.S. FILE is the current file and dirname will strip off the /something.php from the end therefore leaving the path of the directory containing the current file.

Upvotes: 4

hakre
hakre

Reputation: 198119

You can not say in advance if $_SERVER['DOCUMENT_ROOT'] contains a slash at the end or not.

Normally, if properly configured, it does not contain a trailing slash. On Ubuntu (as well as on other UNIX), a properly written path to a directory does not have the / at the end. On windows for example, apache will even refuse to start if it's configured with one. On UNIX Apache is not that picky and allows with a trailing slash.

But there is one exception, if you make your root directory (/) your document root. Because of that case, you can not say in advance whether or not it contains a trailing slash.

In any case it contains the value of the DocumentRoot directive - with or without trailing slash like it has been written into the httpd configuration file. PHP only takes over the value from apache. To get the real document root, use realpath and/or conditionally add a slash (or remove it) at the end if either in your configuration file or within your PHP code.

Upvotes: 12

rekire
rekire

Reputation: 47965

I think that depends on the server configuration if the web root is defined with or without a a tailing slash. Simply check that this is on every system equal.

See also the ServerRoot-Directive of the apache documentation.

Upvotes: 1

Related Questions