PHP XAMPP Server DOCUMENT_ROOT folder structure

bagofmilk picture bagofmilk · Jan 10, 2014 · Viewed 8.9k times · Source

So this is my first time creating a test site with xampp. I originally had all my php files in one folder and just recently decided to organize the data (yes, hindsight i should've started with an organized folder structure.) Anyways, I have my setup like this:

" [ ] " implies it is a FOLDER

Installed on my C:\ drive

[xampp]
└── [htdocs]
    └── [QMS]
        └── [rev3]
            ├── [css]
            ├── [js]
            ├── [DPU]
            ├── [login]
            ├── index.php
            ├── header.php
            └── config.php

In my "config.php" file I tried to define a root path (this may be the error):

$path = $_SERVER['DOCUMENT_ROOT'] . "/QMS/rev3/";

.

Then in my header.php file I have:

<?php
require $_SERVER['DOCUMENT_ROOT'] . "/QMS/rev3/config.php";
include $_SERVER['DOCUMENT_ROOT'] . "/QMS/rev3/login/session.php";
.....
?>

HTML - located in the <head> section

<link rel='stylesheet' type='text/css' href='<?php echo $path . "css/searchBar.css"; ?>'/>
<link rel='stylesheet' type='text/css' href='<?php echo $path . "css/tables/filtergrid.css"; ?>'/>
<script type="text/javascript" language="javascript" src='<?php echo $path . "js/jquery.dataTables.js" ?>'></script>
<script type="text/javascript" language="javascript" src='<?php echo $path . "js/jquery.loader.js" ?>'></script>

... MANY OTHER scripts and stylesheets.

. My index.php is:

require $_SERVER['DOCUMENT_ROOT'] . "/QMS/rev3/header.php";

When I launch this in Chrome - I get the following errors for ALL of my scripts and stylesheets (19 errors total):

"NOT ALLOWED TO LOAD LOCAL RESOURCE  file///C:/xampp/htdocs/QMS/rev3/ ......etc..."

My site was working perfectly when all my files were in the same folder and I wasn't using SERVER['DOCUMENT_ROOT'], but now I have no idea what to do...any advice?

.

Answer

Jasper N. Brouwer picture Jasper N. Brouwer · Jan 29, 2014

NOT ALLOWED TO LOAD LOCAL RESOURCE

The url you use in <a>, <link>, <script>, etc tags should be relative to the document-root, eg:

<link rel="stylesheet" type="text/css" href="/QMS/rev3/css/searchBar.css" />

Don't confuse paths on disk with url's, they're two completely different things :)

Advise on determining the root path

I recommend not to rely on the $_SERVER['DOCUMENT_ROOT'] variable, but determine the root-folder like this (in config.php):

define('ROOT_PATH', __DIR__);

You'll have a constant named ROOT_PATH which will contain C:\xampp\htdocs\QMS\rev3 (without a trailing /).

Now you can do things like:

require ROOT_PATH . '/header.php';

The path in ROOT_PATH differs from the document-root. If you really want to use the document-root, do this (so that ROOT_PATH will then contain C:\xampp\htdocs):

define('ROOT_PATH', __DIR__ . '/../..');

# ...

require ROOT_PATH . '/QMS/rev3/header.php';

Canonicalized absolute pathname

It might be wise to use realpath() as mentioned by Capsule (expand all symbolic links and resolve references to /./, /../ and extra / characters):

$rootPath = __DIR__ . '/../..';  # or just __DIR__
$realPath = realpath($rootPath);
define('ROOT_PATH', $realPath ?: $rootPath);

If realpath() fails to resolve the path it will return false, that's why there's a little check.