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 6 of 6
  1. #1
    Regular Coder
    Join Date
    Dec 2010
    Location
    London
    Posts
    334
    Thanks
    63
    Thanked 11 Times in 11 Posts

    directory problem with php include

    Hi there.

    I may be missing a trick here, but when using an include for my header.php I'm getting some problems with directory folders.

    The problem happens when my current page isn't on the same level as the one I'm linking to, for example my 'gallery.php' is in a sub directory called 'gallery', but when I try to link to 'home.php' from this page, it looks for 'gallery/home.php'.

    I know I can get around this by linking to "../home.php" but as i'm using an include for my header.php then this doesn't work, as I need to link to "home.php" on other pages higher in the directory tree.

    Is there a novel way around this?

    header.php
    Code:
    <div id="header-links" class="header-links">
    	<ul class="inline">
    		<li><a href="landing.php">new search</a></li>
    		<li><a href="gallery/gallery.php">gallery</a></li>
    		<li><a href="venues.php">venues</a></li>
    	</ul>
    </div>

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Yep, this is per normal.
    PHP's cwd comes from the executing script, not from the script requesting an inclusion. As you start stacking inclusions, than the filepaths no longer match up.
    The answer is to always use relative from THIS script (the one calling the include). I don't recommend absolute paths as if you distribute the software that creates a little bit of a mess unless you want to precalculate everything or ask for explicit configurations, and I don't recommend pulling from $_SERVER since that then locks you into a web environment. So to include a path from above *this* script would be a simple:
    PHP Code:
    require_once __DIR__ '/../home.php'
    Now if this script is included into /index.php for example and called with /subdir/thatscript.php, than /subdir/thatscript.php will resolve /subdir/../home.php as the path to include.

    Simply prefixing with __DIR__ and working relative from this file's path eliminates the concern with inclusion depths and where the inclusion is called.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Regular Coder
    Join Date
    Dec 2010
    Location
    London
    Posts
    334
    Thanks
    63
    Thanked 11 Times in 11 Posts
    Thanks a lot for your help Fou-Lu, but after playing around with your answer I'm still quite confused.

    Say my include is this;

    header.php
    Code:
    <ul class="inline">
    	<li><a href="landing.php">new search</a></li>
    	<li><a href="gallery/gallery.php">gallery</a></li>
    </ul>
    And my page I want to include it on is this;

    gallery/gallery.php
    Code:
    <div id="container">
     	<?php require_once __DIR__ . '/../header.php'; ?>
     	<p>more page content</p>
    </div>
    I'm still getting a file path of 'gallery/gallery/gallery.php' if I'm on that page. This will however work perfectly when included on '(no subdir)/home.php'.

    I realise that's because I'm not doing what you told me properly , but could you point me in the right direction?

  • #4
    Regular Coder
    Join Date
    Dec 2010
    Location
    London
    Posts
    334
    Thanks
    63
    Thanked 11 Times in 11 Posts
    I do appreciate you going to the effort of explaining in detail btw, I read through what you said about 10 times but it's just a bit over my head!

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Ohhh wait I think you are looking at this: <li><a href="gallery/gallery.php">gallery</a></li>
    That isn't the same as a PHP include. PHP has no control of this; it is a client specific operation. On the plus side, HTML is jailed to the public_html (or whatever it is configured as), so using /gallery/gallery.php would lock it into the site.com/gallery/gallery.php.
    Using the base tag can also alter this. HTML links are always performed relative to the output as well, but it's not as much of a headache since it always considers the root as the site root and not filesystem root. Plus you cannot stack includes in the HTML world the same way you do in PHP.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    paddyfields (02-12-2013)

  • #6
    Regular Coder
    Join Date
    Dec 2010
    Location
    London
    Posts
    334
    Thanks
    63
    Thanked 11 Times in 11 Posts
    I just added a '/' to the beginning of the link address and it's linking it from the localhost (exactly what i needed!)

    So simple...

    Thanks for your help.


  •  

    Posting Permissions

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