SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    Nov 2011
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Should I use root-relative or document-relative paths when

    I plan to design my website on my machine (which has mysql, php, and other things installed) and then plan to move it to various web servers later on?

  2. #2
    SitePoint Enthusiast abdussamad's Avatar
    Join Date
    May 2006
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What do you mean by root relative and document relative? Also where exactly will you be using these paths. Within PHP files to include other PHP files or in client side HTML code.

  3. #3
    SitePoint Zealot
    Join Date
    Nov 2011
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I will be using them in PHP, when I point to other files not in the document, primarily through includes.
    But the question is also for html, or rather pointing to external stylesheets and javascript code.
    Root relative = /images/image.png
    Document relative (say the current document is in the /text path) = ../images/image.png

    Or at least that's what I think. I am a bit confused myself on this. I read that both have advantages, but I couldn't tell which was best for portability mentioned above. All I know is that absolute paths are out. I do my development on my own pc, then ftp it to various places.

  4. #4
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,206
    Mentioned
    456 Post(s)
    Tagged
    8 Thread(s)
    In my view, by far the best way to to use root-relative links. (Then you don't have to mess around with the paths on each page.) However, this does pose a problem if you are first developing the site locally. The way around it is to set up a local server environment. I'm not sure if that's what you have, but the easy way to do this is to use an all-in-one setup that you can download for free. Two for Windows are WAMP and XAMPP. For Mac, there is MAMP.

  5. #5
    SitePoint Enthusiast abdussamad's Avatar
    Join Date
    May 2006
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There are two separate cases here.

    - PHP includes use the file system so it's best to use the absolute path according to the file system

    - Client side HTML uses the path relative to document root so best to make it root relative

    For example say that the document root is:

    /home/admin/public_html/

    and the website URL is www.example.com


    In the case of the PHP scripts what you would first do is define a global constant, usually in the init.php file or some such, containing the document root:

    Code:
    <?php
    
    //    __FILE__ is the full path of the currently executing php file. This assumes that file is in the document root. 
    define ('DOCUMENT_ROOT', dirname(__FILE__));
    
    
    echo DOCUMENT_ROOT ; //displays /home/admin/public_html
    
    echo DOCUMENT_ROOT.'/includes/functions.inc.php'; //displays /home/admin/public_html/includes/functions.inc.php
    So you can use the DOCUMENT_ROOT constant + root relative wherever you want the absolute file path such as in the case of php include statements.

  6. #6
    SitePoint Zealot
    Join Date
    Nov 2011
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I do develop locally on a server environment (like WAMP). I should have been clearer.

    Thanks though - I think this solves my confusion. In php I do make use of $_SERVER['DOCUMENT_ROOT'] and usually point to files from there. I was a bit afraid that this wouldn't work if I somehow broke it. I was also a little worried about there being potentially more overhead using this method. I am just trying to aim for maximum portability of files from one server to another (or moving from one folder to another), as the programs and html I develop will be deployed in multiple places.

  7. #7
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,206
    Mentioned
    456 Post(s)
    Tagged
    8 Thread(s)
    Quote Originally Posted by ShinVe View Post
    In php I do make use of $_SERVER['DOCUMENT_ROOT'] and usually point to files from there. I was a bit afraid that this wouldn't work if I somehow broke it.
    It should work fine. I've never had a problem with it, except that in WAMP etc your document root might be different. You can make the folder your files are in seen as the root folder by tweaking your hosts file.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •