#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    Boston, MA USA
    Posts
    388
    Rep Power
    47

    Consistent Path Usage


    Hi:

    I'm trying to organize the files for a web site into sub-directories but I'd like them to to still understand where included files are. The problem is that the path to various files is different from directory to directory or from server to server. The biggest problem is that when one included file, a header, let's say, includes another file, a css, then the relative paths are totally inconsistent if referenced from files in different directories.

    For example, files which are included on all pages are located in /includes.

    From the home page the path is simply "includes/[filename]"

    but from pages in subfolders it will be "../../includes/[filename]"

    I thought I could use $_SERVER['DOCUMENT_ROOT'] for this purpose by setting:
    $default_path = $_SERVER['DOCUMENT_ROOT'];
    and then all paths would be "$default_path/includes/[filename]" regardless of which directory they are in or which server they are on.

    The problem is that that path resolves differently on each server:
    path my development (local) server is "localhost/domainname/includes/[filename]"
    path on the actual domain "domain name.com/includes/[filename]"
    Using $_SERVER['DOCUMENT_ROOT']:
    On my local server: "/Applications/XAMPP/xamppfiles/htdocs/domainname/includes/[filename]"
    On the domain: "/home/users/web/b385/username/includes/[filename]"

    How can I set a variable for the "root" directory that will be universally understood?

    Thanks
    Last edited by SeanF; April 28th, 2013 at 08:09 AM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,489
    Rep Power
    594
    By design all paths are either relative to the current document directory or absolute with respect to DocRoot. To accomplish what you want simply use absolute paths (e.g. /includes/[filename]).
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    Boston, MA USA
    Posts
    388
    Rep Power
    47
    Originally Posted by gw1500se
    By design all paths are either relative to the current document directory or absolute with respect to DocRoot. To accomplish what you want simply use absolute paths (e.g. /includes/[filename]).
    That doesn't work between servers:

    On the production server, "/" resolves to www.domainname.com/

    On my localhost, "/" resolves to:
    /Applications/XAMPP/xamppfiles/htdocs/domainname

    I suppose I could hard code that in with a logic statement based on the ''SERVER_ADDR" but that seems clumsy.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,489
    Rep Power
    594
    Make DocRoot on the servers consistent. Why do you have the incudes on one server in DocRoot and someplace completely different on another?

    That being said the rest of your post is confusing. Using $_SERVER['DOCUMENT_ROOT'] will indeed solve your problem regardless of what it resolves to, if your directory tree on each server is the same with respect to DocRoot.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    I am fairly confused at this point; when discussing paths, keep in mind that there are different types of paths:
    1) A filesystem path - the root of which (/) is the root of the server's filesystem.
    2) The HTTP request path - the root of which (/) is your document root.

    If you're using an include() or require() construct, you use #1. If you're outputting a URL into the page for a link/css file/js file/image/etc. you use #2.

    Without some element of guessing, $_SERVER['DOCUMENT_ROOT'] is only useful for determining #1 (and even in that case, it's not useful 100% of the time).

    Major PHP frameworks solve this problem by defining a configuration variable which contains either the URL to the site or just the path (#2) to the root of the application. This variable is defined in the same way as any other variable.

    Major PHP framesworks also use a single front-controller, which is responsible for setting that variable. Any paths that point at subdirectories are handled using URL rewriting so that the front controller is always invoked.

    If you don't have a front-controller, it's a little more difficult to do this; however, you should be able to create a configuration file and include it on every page using $_SERVER['DOCUMENT_ROOT'].
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    Boston, MA USA
    Posts
    388
    Rep Power
    47
    Originally Posted by gw1500se
    Make DocRoot on the servers consistent. Why do you have the incudes on one server in DocRoot and someplace completely different on another?

    That being said the rest of your post is confusing. Using $_SERVER['DOCUMENT_ROOT'] will indeed solve your problem regardless of what it resolves to, if your directory tree on each server is the same with respect to DocRoot.
    The directory tree is identical on both servers from the index file (home) down. The difference is from the home directory up, which I can't control enough to make identical.

    On the production server, my hosting company hosts the domain in a folder called [username] which is not necessarily the same as [domainnname]. So, while the URL of the domain is http://www.domainname.com/index.html" the location of the file is "/home/users/web/b385/username/index.html" (I can't control anything above "index.html"

    On my development server, using XAMPP, each domain that I happen to be working on is loaded "localhost" so for one domain the root is actually "http://localhost/domain1/" and for another it's "http://localhost/domain2/".


    $_SERVER['DOCUMENT_ROOT'] on my local server resolves to:
    /Applications/XAMPP/xamppfiles/htdocs

    But on the actual domain it resolves to:
    /home/users/web/b385/[username].

    I've tried simply using $_SERVER['DOCUMENT_ROOT'] in a path and it does not work, so I need to use a condition that reads $_SERVER['DOCUMENT_ROOT'] and based on the value, either sets $domain_root to either "http://www.domainname.com/" or "http://localhost/domainname"

    ... unless I'm missing something
    Last edited by SeanF; April 28th, 2013 at 09:24 AM.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,489
    Rep Power
    594
    You must be missing something. If both directory trees are identical with respect to DocRoot, it will work. Unfortunately, "it does not work" is not enough information to help you much. Did you echo DocRoot on both servers to make sure it does indeed point to what you expect and did you do an 'ls' or 'dir' on both DocRoots to make sure the trees are indeed identical?
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    Boston, MA USA
    Posts
    388
    Rep Power
    47
    Thanks for your various replies...

    Here's a specific example that doesn't work. I have organized web pages in subdirectories that pertain to categories of topics. There is an "includes" directory at the top level that include files used by all pages such as headers, footers, menus, etc. There is a "styles" directory at the top level that includes all css files. the header files in the "includes" directory calls the css files in the "styles" folder.

    When a file at the top level such as index.php calls the header the path is simply down one level, i.e. includes/header.inc.php. The header file then calls the css but, since the header is actually part of index.php, the path to the css file, again is straight down, i.e. "styles/style.css".

    But, is a file is in a subdirectory, i.e. /category1/category1.php, it calls the header file as up a level and then down, i.e. ../includes/header.inc.php. Again, the header file is actually part of category1.php so the correct path to the css would be "../styles/style.css" so the same include statement (in the header file) won't work if the header file is called from scripts in different directories.

    Similarly, I put the following in the header file:
    Code:
    $full_path = $_SERVER['SCRIPT_FILENAME'];
    echo "Full Path from header= $full_path<br>";
    When I view the file on my local development server, I see:
    Path from header= Full Path from header= /Applications/XAMPP/xamppfiles/htdocs/[domainname]/index.php

    When I view the file on the production server, I see:
    Path from header= /home/users/web/b385/[username]/index.php

    Which tells me that trying to declare paths from the root, i.e "/" would also require different paths.

    Domain directory structure:

    /index.php
    /includes/header.inc.php (called by all pages regardless of directory)
    /styles/stye.css (called by header.inc.php)
    /category1/category1.php
    /category2/category2.php
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    Boston, MA USA
    Posts
    388
    Rep Power
    47
    Originally Posted by E-Oreo
    I am fairly confused at this point; when discussing paths, keep in mind that there are different types of paths:
    1) A filesystem path - the root of which (/) is the root of the server's filesystem.
    2) The HTTP request path - the root of which (/) is your document root....
    Thanks for this reply... I did not see it earlier...

IMN logo majestic logo threadwatch logo seochat tools logo