bagofmilk
bagofmilk

Reputation: 1550

PHP XAMPP Server DOCUMENT_ROOT folder structure

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?

.

Upvotes: 2

Views: 12548

Answers (5)

user4710756
user4710756

Reputation:

After trying a lot of solutions i made like this, because it's the way it work local and online. You can define a variable with the path you need like this :

$path = "";
if ($_SERVER['HTTP_HOST'] == "localhost")
{
    $path = $_SERVER['DOCUMENT_ROOT']."/mysite";
}
else
{
    $path = $_SERVER['DOCUMENT_ROOT'];
}

include_once($path.'/include/config.php');

Upvotes: 0

Marco Sanchez
Marco Sanchez

Reputation: 788

Try "QMS/rev3/header.php" without the "/" at the begining

Upvotes: 1

Shakeel Ahmed
Shakeel Ahmed

Reputation: 1848

Do not use document_root for defining main path. Put this line of code in your config.php

$path = rtrim(dirname(__file__),"/")."/";

Now your path variable has your root path including leading slash (/). You can use this.

Upvotes: 1

Jasper N. Brouwer
Jasper N. Brouwer

Reputation: 21817

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.

Upvotes: 3

Itay Gal
Itay Gal

Reputation: 10834

If by removing the / from this line

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

you manage to include the header.php file then you also need to change $path to:

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

In general - if you use a config.php to set your $path and you include it in all of your pages as you should, then use the $path variable and not $_SERVER['DOCUMENT_ROOT'] . "QMS/rev3/. This way you can mantain your code easily.

About the NOT ALLOWED TO LOAD LOCAL RESOURCE, take a look at this answer

Upvotes: 1

Related Questions