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

Posted on 2008-11-06
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 107

    Accepted Solution

    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
    There are two different directory structures involved. Let look at a typical set up

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

    To the webserver the images directory is at

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

    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

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
    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…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now