I have a pretty stable development machine set up running Apache and using virtual hosts to keep my projects separate, and running a dyndns.org service which I use to access them. Each VHost directive typically looks like this:

<VirtualHost *:80>
    ServerName [my_internal_subdomain].[my_dyndns_name].dyndns.org
    ServerAlias home

    DocumentRoot "D:/webserver/[projectDirectory]/httpdocs"
    php_admin_value open_basedir "c:/WINDOWS/TEMP;D:/webserver/[projectDirectory]/"

    ErrorLog "D:/webserver/[projectDirectory]/logs/error.log"
    TransferLog "D:/webserver/[projectDirectory]/logs/access.log"

Obviously [projectDirectory], [my_internal_subdomain] and [my_dyndns_name] are all values I know/change for each directive, just no need to post them here :)

One thing has always confounded me however, in PHP, if I want to require a file from say /includes/, I would expect to use:


Except doing so throws an open_basedir restriction error - because "/" is trying to go, presumably to the root of D:/ - whereas I would like each virtual host to recognise that the value of DocumentRoot is in fact where I would like PHP to go if I call a file by "/".

This is how my production server seems to work, and its a bit of a pain having to code everything to work out what the relative path to /includes/ is for the benefit of my dev machine.

I expect this is a fairly simple/obvious directive I'm missing but I can't find it. Can anyone help?


Thanks for the answers so far. To clarify, I do presently use a PHP based method similar to those suggested to include files from the right path, but I figured there had to be a better way. I'm Pretty sure "/" works on my production server (which was configured by my service provider and administered using Plesk), but that runs linux, and probably has the vhosts jailed as I provide shared hosting on it.

Written by Codecraft

Accepted Answer

Do you mean this?

require($_SERVER['DOCUMENT_ROOT'] . '/includes/myfile.php');
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki