Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    Regular Coder
    Join Date
    Jun 2007
    Posts
    805
    Thanks
    123
    Thanked 23 Times in 23 Posts

    Always make paths relative to root

    Hi,

    My site is split up in includes. In one include a link to the css file is placed. The images in the css file are relative to my root like this:

    Code:
    background-image:url(../images/background.jpg);
    However, when the CSS file is included in another page deeper in the hierarchy, the image path in the CSS file does not fit anymore thus the image doesn't display.

    Isn't there a way to define all paths relative to the root so that it will work no matter where the CSS is linked from??
    Last edited by CaptainB; 04-11-2009 at 05:05 PM.

  • #2
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Hmm, I might be wrong, but I always thought that CSS-called images were called up based on the path in the CSS (in other words, in a path relative to where the CSS file itself is located, regardless of where the HTML or PHP page is located).

    Am I wrong about this or is there a different way that it works for PHP? I'm not very PHP savvy just yet so I'm curious...
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • #3
    Regular Coder
    Join Date
    Jun 2007
    Posts
    805
    Thanks
    123
    Thanked 23 Times in 23 Posts
    * deleted - my mistake *
    Last edited by CaptainB; 04-10-2009 at 10:17 PM.

  • #4
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    That is strange. Also, I didn't read your post carefully enough to notice the question at the bottom (duh!)

    And the answer is this:
    Code:
    background-image:url(/images/background.jpg);
    You just drop the ".." from the front of the string.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • Users who have thanked Rowsdower! for this post:

    CaptainB (04-10-2009)

  • #5
    Regular Coder
    Join Date
    Jun 2007
    Posts
    805
    Thanks
    123
    Thanked 23 Times in 23 Posts
    No wait, I am mistaken, it is like you say! Sorry for that!
    Hold on - trying

  • #6
    Regular Coder
    Join Date
    Jun 2007
    Posts
    805
    Thanks
    123
    Thanked 23 Times in 23 Posts
    That's how it goes when you're really not aware what you're asking.... (I might have been sitting here for too long).

    The question really does not have anything to do with my css file (doh).
    This is the case:

    In my page I have a link to an image:
    Code:
    <img src="images/image.png" alt="image" />
    It works good when the page is located in the root. However, when the page is moved further down:

    Code:
    Root --> Folder1 --> Page.php
    the image does not show. Of course. But, when the link is changed to this (slash added):
    Code:
    <img src="/images/menu.png"
    nothing shows at all - even not at the page in the root where it showed before. That's problematic as the link is included in every page and therefore has to be relative to it all, if you know what I mean. How would I come around that?

  • #7
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    nothing shows at all - even not at the page in the root where it showed before.
    Well that simply shouldn't be. Do you have a link to your page so I can take a look and make sure there are no other issues at play?
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • #8
    Regular Coder
    Join Date
    Jun 2007
    Posts
    805
    Thanks
    123
    Thanked 23 Times in 23 Posts
    Stby -
    Last edited by CaptainB; 04-10-2009 at 10:38 PM.

  • #9
    Regular Coder
    Join Date
    Jun 2007
    Posts
    805
    Thanks
    123
    Thanked 23 Times in 23 Posts
    I fixed a couple of errors relating to my site being in an actual test map and NOT in the server root - as result this will be my last post tonight.

    Now, with that fixed, all images show regarding of the location of the file. It worked with the leading slash as you said!

    However, none of my actual includes ARE getting included when their paths have a leading slash -
    link(page in root): http://behrentzs.com/1.php
    link(page outside root): http://behrentzs.com/svmanager/

    Code example (does not load into page):
    PHP Code:
    <?php include("/includes/header.php"); ?>
    Code example (does load into page):
    PHP Code:
    <?php include("includes/header.php"); ?>
    Considering the page is in the root of the server. None of the above loads includes into pages placed outside the root.

  • #10
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    With PHP you are out of my area of "expertise" (if I can call myself an expert in anything web-related).

    I'm not sure if there is anything syntactically wrong with using the leading slash for filepaths on PHP includes or not. I can't see any reason why there would be any problem using that, but I am such a PHP novice that my judgment on this should be taken with a grain of salt.

    If you don't get any better-informed responses to your question here you may want to ask a moderator to move this thread over to the PHP section or else re-post there (since the start of this thread wasn't really PHP-related to begin with).

    Before any of that though, have you searched google or the archives here for this yet? There might be an explanation out there already.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • #11
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,852
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    PHP Code:
    <?php include("/includes/header.php"); ?>
    When dealing with PHP, the absolute path starting with / may not be like the path in the client side(browser).

    From the serverside, / stands for the document_root, which can be identified by echoing $_SERVER['DOCUMENT_ROOT']. Thus if your includes directory is not directly under your document_root, you may define a named constant at the top of your file( say inside a config.php file, if exists), like
    PHP Code:
    define(_ROOT_$_SERVER['DOCUMENT_ROOT']."/directory/subdirectory"); 
    and then easily include your PHP files like
    PHP Code:
    include _ROOT_."/includes/header.php"
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #12
    Regular Coder
    Join Date
    Jun 2007
    Posts
    805
    Thanks
    123
    Thanked 23 Times in 23 Posts
    Upon echoing the server root I get this:

    Code:
    customers/behrentzs.com/behrentzs.com/httpd.www
    This is what I inserted between my <head> and </head>:
    PHP Code:
    <?php 
    //Config file - configures the document root and thus the file path used in includes
    define(_ROOT_$_SERVER['DOCUMENT_ROOT']."/customers/behrentzs.com/behrentzs.com/httpd.www"); 
    ?>
    This is what I have in my PHP:
    PHP Code:
    <?php include(_ROOT_."/includes/header.php"); ?>
    But with no result unfortunately.

  • #13
    Regular Coder
    Join Date
    Jun 2007
    Posts
    805
    Thanks
    123
    Thanked 23 Times in 23 Posts
    This worked with the includes:

    PHP Code:
    <?php include($_SERVER['DOCUMENT_ROOT']."/includes/header.php"); ?>


  •  

    Posting Permissions

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