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

Posted on 2008-11-06
Medium Priority
Last Modified: 2008-12-17
My folder structure is like this


- myfile.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.



Question by:Hulfy
LVL 111

Accepted Solution

Ray Paseur earned 668 total points
ID: 22896011
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

LVL 34

Assisted Solution

by:Beverley Portlock
Beverley Portlock earned 668 total points
ID: 22896027
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>
LVL 11

Assisted Solution

jausions earned 664 total points
ID: 22896039
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.

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month14 days, 15 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question