tags:

views:

4981

answers:

7

On various pages throughout my PHP web site and in various nested directories I want to include a specific file at a path relative to the root.

What single command can I put on both of these pages...

http://www.example.com/pageone.php
http://www.example.com/somedirectory/pagetwo.php

...to include this page:

http://www.example.com/includes/analytics.php

This does not work:

<?php include('/includes/analytics.php'); ?>

Does it matter that this is hosted in IIS on Windows?

+1  A: 

Use .. to go up a directory. So in pageone.php

include 'includes/analytics.php';

in pagetwo.php

include '../includes/analytics.php';

There's no notion of "root" as you're referring to in PHP as far as I know, though you could define it if you wanted. Best of luck!

Stefan Mai
There's no way to use an absolute path?
Zack Peterson
You can use an absolute path like "c:/includes/analytics.php" or "/home/iamnafets/includes/analytics.php", but PHP files aren't part of a "project" or "virtual directory" like ASP pages are.
Stefan Mai
I wasn't specific enough. I want a single command that will work from either page.
Zack Peterson
Not without hard-coding a path (c:/.... or /var/...) or defining a root yourself
Greg
You can change what directories PHP will search with the include_path directive. Have a look at http://stackoverflow.com/questions/344419/how-do-i-set-an-absolute-include-path-in-php
gnud
+1  A: 

As @Stefan Mai says, PHP doesn't have a "root" path but you can define one quite easily - most sites have a page that's included every time (e.g. configuration file), to which you can add:

define('ROOT', dirname(__FILE__));

Then use include ROOT . '/includes/analytics.php';

Something that's also quite useful is the auto_prepend directive, which you can use in .htaccess on apache - not sure about setting it up on IIS (although you can have a global one in the PHP ini).

Greg
of COURSE php has a root. It's the filesystem root!
gnud
+5  A: 

You can just use include $_SERVER['DOCUMENT_ROOT'] . "/includes/analytics.php";

J Cooper
Does not work under IIS
John JJ Curtis
+2  A: 

If you give include() or require() (or the *_once versions) an absolute pathname, that file will be included. An absolute pathname starts with a "/" on unix, and with a drive letter and colon on Windows.

If you give a relative path (any other path), PHP will search the directories in the configuration value "include_path" in order, until a match is found or there are no more directories to search.

So, in short, to include an absolute filename, give an absolute filename. See also the function realpath().

If you want to set your own include "root", have a look at this question (specifically my answer of course :-)

gnud
A: 

The tilde is interpreted as a special character by the shell, so using it inside PHP code won't work regardless of OS.

If you're trying to access something relative to a user home directory you could try getenv() - I'm pretty sure Windows sets an environment variable equivalent to $HOME.

Ant P.
A: 

This works.

<?php include('c:/inetpub/example.com/includes/analytics.php'); ?>

I'll have to make the c:/inetpub/example.com/ part some kind of global variable so it's somewhat portable from server to server.

Zack Peterson
This will cause problems when you move your site to another server.
Jacco
+1  A: 

From the perspective of PHP root is the top of the file system on the web server, not the root of the from the perspective of the web browser.

Most people do one of the below.

Define a constant, in a global configuration file, and use that in each call to require/include.

Or they use code like this.

require_once realpath(dirname(__FILE__).'/config.php');
require_once realpath(dirname(__FILE__).'/lib/Database.php');

Using the environmental variables may be dangerous in some cases and be the source of security issues.

Zoredache