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 4 of 4

Thread: Relative paths

  1. #1
    New Coder
    Join Date
    Jun 2007
    Location
    Canada
    Posts
    49
    Thanks
    1
    Thanked 1 Time in 1 Post

    Relative paths

    Running into problems with relative paths for various files. I'm trying to segregate related scripts into their own folders. ie header & footer into a template folder. admin functions into an admin folder etc...

    I'm running into the most basic problems such as - a page in the admin folder includes the header file.
    Code:
    include("../template/header.inc.php");
    The header file is being included ok but the CSS link inside the header file isn't correct.
    Code:
    echo '    <link rel="stylesheet" type="text/css" href="' .$APP_BASE. '/template/screen.css" media="screen" />';
    $APP_BASE is created using
    Code:
    $APP_BASE = '//' . $_SERVER['HTTP_HOST'] . rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
    The generated CSS link turns out as:
    Code:
    <link rel="stylesheet" type="text/css" href="//nexus/raw/admin/template/screen.css" media="screen" />
    How do I create these links properly? I can't use "../template/screen.css" because I have a few general files which are in the application root. Using the "../" causes the file path to be in the server root in that case.

    Appreciate the help.
    Of all the things I've lost in my life time, my mind is the one I miss the most!

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,027
    Thanks
    2
    Thanked 315 Times in 307 Posts
    ... the CSS link inside the header file isn't correct.
    You did not actually state what was wrong with that CSS link (other than it should have a http: before the // to make it a properly formed, fully specified, URL.)

    But, I will guess that because dirname($_SERVER['PHP_SELF']) is the path to the file that is including the header and that this changes depending on which file is including the header file, that you are getting a varying path to the css file?

    Don't use the $_SERVER['PHP_SELF'] in this situation. If you are trying to make an absolute URL to the css file, just do that -
    PHP Code:
    $APP_BASE 'http://' $_SERVER['HTTP_HOST'];
    ...
    echo 
    '<link rel="stylesheet" type="text/css" href="' .$APP_BASE'/your_actual_full_path_here_to_the/template/screen.css" media="screen" />'
    Also, because files that the browser fetches directly (images, external javascript, external css...) use the / = "domain root" rule, you can also just use -
    PHP Code:
    echo '<link rel="stylesheet" type="text/css" href="/your_actual_full_path_here_to_the/template/screen.css" media="screen" />'
    Edit: Also, take a look at the __FILE__ magic constant - http://www.php.net/manual/en/languag...predefined.php if you want to get the path to the included file instead of the file that is doing the including.
    Last edited by CFMaBiSmAd; 11-16-2007 at 05:41 AM. Reason: add magic constant info
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    Regular Coder
    Join Date
    Apr 2003
    Location
    Montreal, QC
    Posts
    340
    Thanks
    3
    Thanked 2 Times in 2 Posts
    I think you have 2 options:

    1) Add more to the href that calls the CSS file. I was going to say to use $_SERVER['HTTP_HOST'] - but you are already. However, it doesn't appear to be creating any output. You'll need to figure out why - I have no idea at this late hour

    2) don't bother putting the domain name - just use a directory path starting with "/" So, if your CSS file is at http://www.example.com/templates/style.css then your link tag should have href="/templates/style.css" and then you may be able to leave PHP right out of it, (this is what I usually do).
    Search for Laughter or Just Search?
    GiggleSearch.org
    Blog: www.johnbeales.com
    All About Ballet: www.the-ballet.com

  • #4
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Or just use the base tag.


  •  

    Posting Permissions

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