tags:

views:

302

answers:

4

I'm brand new to php and I'm trying to work with some includes in my site and can't quite figure out how to make them all work correctly. My site structure is as follows

/ROOT/
   Config.php
   Index.php
   /ADMINISTRATION/
      Index.php
      mustInclude.php
      /USERS/
         Index.php

If "mustInclude.php" includes "Config.php" and Index.php includes "mustInclude.php" everything works fine, but as soon as I try to include "mustInclude.php" into /USERS/Index.php it breaks because "mustInclude.php" is using a path like include '../config.php'; and that isn't the same relative path for /USERS/Index.php as for /ADMINISTRATION/Index.php

I'm not really sure what to do here.

This is on my local machine for now. Using $_SERVER['DOCUMENT_ROOT'] gives me errors because it outputs my file structure (/Users/James/Sites) rather than my web structure (http://localhost/mysite)

Help?

+3  A: 

I suggest defining some kind of "global base path" and deducing the other paths from there. Like,

define('BASE_PATH', '/home/me/');
...
include(BASE_PATH . 'Config.php');
include(BASE_PATH . 'subdirectory/other.php');

and so on. This kind of (semi)absolute paths are less fragile than relative paths containing lots of ../s. (Not to say that there's anything fundamentally wrong with relative paths, but they're harder to get just right and tend to break more easily. If /a includes b/c and b/c includes ../d/e, is that relative to /b/ or relative to /, or does it depend on whether we started from /a vs. called b/c directly? I don't even know. Better just use the absolute paths, they're easy and unambiguous :-).

Joonas Pulakka
+2  A: 

Instead of using '../config.php' try using

dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'config.php'

This way you will always know that one level above is one level above from the current file.

Andrei Serdeliuc
A: 

you can use __FILE__

Anas Toumeh
anyone care to explain why this poster was voted down? I'd really like to learn the good and the bad side of php.
Pselus
I guess it was voted down because of its brevity. Use "_FILE_"? What is it? How to use it? Essentially this answer is (likely) the same as Aprikot's answer above, but quite poorly described.
Joonas Pulakka
I'm sorry I didn't have the time to write a full post but it was posted before Aprikot's answer above which is the right answer.
Anas Toumeh
+3  A: 

IMO the best way to include files from anywhere in your application directory structure is to add the root folder of your app to your PHP include path:

<?php
set_include_path(get_include_path().PATH_SEPARATOR."/Users/James/Sites/app_name");

Then, just include files using their paths relative to the application root folder, for example:

<?php
require_once('ADMINISTRATION/USERS/Index.php');
Ben James
I'm not sure it's the *best* way. It could lead to potential ambiguousness, since names like 'ADMINISTRATION/USERS/Index.php' are not fully qualified. You could have '/a/x.php' and '/b/x.php' - put '/a/' and '/b/' to the include path and guess which x "include 'x.php'" refers to...
Joonas Pulakka
Could you explain this a little more please? Where do I need to run this code from? Each file that has the includes? Once in my index.php?
Pselus
Pselus: this needs to be done for every script run, since it does not permanently change the include path for future scripts.
Ben James
Joonas: This is really not a problem in practice, unless you already have other apps set in your include_path variable in php.ini. If for example PEAR is on the include path, then you just avoid naming your directories the same as a PEAR module, which would be silly anyway.
Ben James
Granted, it may not be a huge problem, but there are PEAR packages with quite generic names like "Cache", "File" and "Log". I would rather not have to remember what I shouldn't name my directories.
Joonas Pulakka