PHP doesn't access root directory using forward slash "/"

My folder structure is like this

maindir
-index.php

subdir
- myfile.php

common
-header.php
-footer.php



Now when I have to include my header and footer in myfile .php i use

include('../common/header.php'); ?>

and for index I use

include('common/header.php'); ?>


I am going to have more sub directories and to keep things neat I would  like to define a common root directory and instruct php to look from there and not the directory the file is in.

I can do this with HTML by putting a forward slash, how can this be achieved with php.

BTW I am on a mac 10.5.5 running MAMP as my localhost.

Thanks,



H.



HulfyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
Look into getcwd() and $_SERVER["DOCUMENT_ROOT"] - you can use these to sort it out very easily.

Here is what I use to get the common configurator file for my other directories.

Best, ~Ray
$config_page	= $_SERVER["DOCUMENT_ROOT"] . '/' . '_config.php'; require_once($config_page);

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Beverley PortlockCommented:
There are two different directory structures involved. Let look at a typical set up

A website www.example.com which is hosted on a server at /www/htdocs/ex. It has a subfolder called "images".

To the webserver the images directory is at http://www.example.com/images

To the server the images directory is at /www/htdocs/ex/images

There are two "root" directories and they do not match up. The website's "root" starts after the domain so to it /images means http://www.example.com/images whereas to the server /images is a top level folder.

The way round this is to define things relative to the webroot, but for URLs expand them to a full URL. It sounds messy, but it is sure to work. So use

define ('IMAGES', '../path/to/images');
$img = "<img src='". IMAGES ."'/myimage.jpg />";

$link = "<a href='http://{$_SERVER['HTTP_HOST']}/'>My link</a>
jausionsCommented:
I'm going to assume you're using a configuration file in your setup.

Let's assume you're placing it in "common/" folder: "common/config.php" for instance.

Load that file in each scripts:

require '../common/config.php';

and in "config.php", you could define a constant:

define('BASE_FOLDER', realpath(dirname('..'.DIRECTORY_SEPARATOR.__FILE__)));

Then you'd be able to use BASE_FOLDER in other files, regardless of their location.

include BASE_FOLDER .'/subfolder/path/file.php';


You could also include the "config.php" by a simple:

require 'config.php';

provided you setup your include_path to include the full path to the common/ folder.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.