• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 512
  • Last Modified:

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.



0
Hulfy
Asked:
Hulfy
3 Solutions
 
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

0
 
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>
0
 
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now