Results 1 to 4 of 4

Thread: include relative

  1. #1
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default include relative

    Is there a way to make extensions within an include relative to the path they were included in?

    For example

    PHP Code:
    <?php include("../header.php");?>
    which contains
    PHP Code:
    <link href="css/home.css" rel="stylesheet" type="text/css"
    when it should also be

    PHP Code:
    <link href="../css/home.css" rel="stylesheet" type="text/css"
    Corrections to my coding/thoughts welcome.

  2. #2
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    Try using something along the lines of this:
    Code:
    $root="$_SERVER[DOCUMENT_ROOT]";
    include $root.'/css/home.css';
    To choose the lesser of two evils is still to choose evil. My personal site

  3. #3
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    I'm not including the css file, I'm including a file in another directory with links to css and js in that dir as well.
    Corrections to my coding/thoughts welcome.

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Here are some tricks:
    include(dirname(__FILE__).'/file.php');
    That will include a file relative to the current file, regardless of whether the file is the original location for the request or not.

    For HTML, just use a HREF/SRC starting in '/':
    <... src="/dir1/dir2/....">
    That's not as efficient as a relative path, but it will always work regardless of how it was included into a page.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

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
  •