Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Joey L.
4,601 PointsInclude doesn't work for sibling directory using include("../siblingdir/function.php");
Hello, I'm currently on yahoo webhosting and I'm trying to include a gen_links.php file within my nav-link.php file, which is in a sibling folder. Here is the layout:
- root/functions/gen_links.php
- root/includes/nav-link.php
- root/index.php
Whenever I try
<?php include("../functions/gen_links.php"); ?>
within nav-link.php it doesn't load the main page - which includes the nav-link.php. I'm guessing this is because I call the gen_links() function within nav-link.php but for some reason it cannot load the function.
Strangely though, if I move the gen_links.php file within the same directory as nav-link.php and type:
<?php include("gen_links.php"); ?>
it works just fine. So the function itself does not have an error.
Similarly I tried this:
- /root/test.php
- /root/includes/nav-links.php
- /root/index.php
test.php had the code
<?php echo "This is a test"; ?>
This did not work when I included it via
<?php include("../test.php"); ?>
within nav-links.php
Can anyone shed some light why the relative paths for going above a directory could not be working?
5 Answers

Andrew Shook
31,709 PointsJoey, in PHP include calls are relative to the calling script. that's why I asked you earlier what file the include was called in. I take it that the first file called in your web app is the index.php, and the the index.php calls the nav-links.php. This means that anything included by nav-links.php will use the relative path of the index.php, i.e. it would work as if you's just written the code from nav-link.php into the index.php your self. I would recommend creating a constant in your index.php or settings/config.php file like this"
DEFINE("ROOT_PATH", dirname( __FILE__ ) ."/" );
then included everything using the that constant.
include( ROOT_PATH . "functions/gen_links.php");
tl;dr PHP's include function basically copies code from one file and inserts it into another. So if your includes look like this :
file1->file2->file2
Where file1 includes file2, and file2 includes file3. The the include path for file3 in file2 must be relative to file1 since both files will be included as part of file1.

Andrew Shook
31,709 PointsJoey, what file are you calling the require functions in?

Joey L.
4,601 PointsThe gen_links.php file has the function
gen_links($links)
. I am calling the function within the nav-links.php file, where I include the gen_links.php file via:
<?php include("../functions/gen_links.php"); ?>
This function only works when it is in the same directory.

Chris Shaw
26,676 PointsHi Joey,
I think you're misunderstanding how paths work in PHP as what you have doesn't need to be there if the files exists in the same directory, for example say I have the following structure.
/inc
/functions.php
/something_else.php
/index.php
If say in functions.php
I wanted to include something_else.php
all I would need to do is the below.
include('something_else.php');
PHP isn't like a language such as CSS where you have to back folders and all over the place, in your case all you would need to do is the below.
include("gen_links.php");
And that's it, happy coding =)

Joey L.
4,601 PointsThank you for your reply Chris. Yes that does seem to work, but I don't want to have this function in the same directory, but rather a sibling directory called /functions/.
The structure is:
- root/functions/gen_links.php
- root/includes/nav-links.php
- root/index.php
If I type
<?php include("../functions/gen_links.php"); ?>
within nav-links.php, it doesn't seem to access the gen_links.php file stored in root/functions/.
I copied the same gen_links.php file into the root/includes/ folder where nav-links is and included it with
<?php include("gen_links.php"); ?>
This worked just fine, which means that my function code does not have any errors.
I do not however wish to keep gen_links.php in the same directory, and would much rather include it from root/functions/gen_links.php but typing
<?php include("../functions/gen_links.php"); ?>
does not seem to work.

Chris Shaw
26,676 PointsAh, I misread your first post, in that case you would use the following instead.
include("../includes/gen_links.php");

Joey L.
4,601 PointsNo what I mean is:
- gen_links.php is at root/functions/gen_links.php
- nav-links.php is at root/includes/nav-links.php
- index.php is at root/index.php and loads nav-links.php from includes/nav-links.php
I want to include gen_links.php inside nav-links.php. The code:
<?php include("../functions/gen_links.php"); ?>
Does not seem to work. It causes an incomplete loading of index.php. If gen_links.php is in the same folder as nav-links.php (includes/) and I include it via
<?php include("gen_links.php"); ?>
The index.php file, which includes:
- includes/nav-links.php
Fully loads, and the site is completely functional
Joey L.
4,601 PointsJoey L.
4,601 Points"This mean that anything included by nav-links.php will use the relative path of the index.php."
This actually worked! Thank you! I used
<?php include(functions/gen_list.php); ?>
As opposed to:
<?php include(../functions/gen_list.php); ?>
This has however left me with some confusion as to how includes work. " This means that anything included by nav-links.php will use the relative path of the index.php. " because obviously index.php is calling nav-links from includes/nav-links.php
But if this is true, then why can nav-links.php call files from the same directory like gen_list.php when I stored it there by using its own relative path:
<?php include(gen_list.php); ?>
Instead of:
<?php include(includes/gen_list.php); ?>
as mentioned in " This means that anything included by nav-links.php will use the relative path of the index.php. " The second example works too, and is probably the most foolproof method to use, but I'd like to know anyhow.
Could you please help clarify this? Cheers
Andrew Shook
31,709 PointsAndrew Shook
31,709 PointsJoey, I think I know thats happen but could you please tell me where all the files are currently located? In your comment you have gen_list.php here:
<?php include(functions/gen_list.php); ?>
and here:
<?php include(includes/gen_list.php); ?>
So is it in the includes folder or the functions folder?
Joey L.
4,601 PointsJoey L.
4,601 PointsI had it in both. The original and a copy (I would rename them to avoid their use to test each other separately). Within nav-links.php both
<?php include("functions/gen_list.php"); ?> (Relative to index.php)
and
<?php include("gen_list.php"); ?> (Relative to nav-links.php)
would work. From my understanding the foolproof method for this working by typing the path relative to the last file in the include chain (index.php in my case) like Method 1. I wonder though, why the directory-relative path for nav-links.php in Method 2 was accepted and not:
<?php include("../functions/gen_list.php"): ?> (Relative to nav-links.php)
I don't mind using the foolproof method. I'm curious why method 2 is relative to nav-links.php and not index.php (since the content of nav-links is ultimately copied/included into index.php)
Andrew Shook
31,709 PointsAndrew Shook
31,709 PointsOk so you had one in the functions folder and one in includes folder?
Andrew Shook
31,709 PointsAndrew Shook
31,709 PointsOk I see what is happening. The reason you can still can gen_list.php without telling PHP it is in the functions directory is because the include function has a fallback search in case the file can't be found at the location you gave it. So in your example you are trying to included gen-list.php, so you type this:
<?php include(gen_list.php); ?>
When the PHP interpreter hits this line it says to itself, "Oh, the gen-list.php must be in the current directory relative to the index.php". So PHP looks for the gen-list.php in the same directory as index.php, and fails to find the file. When PHP fails to find an included file at the path you supplied, it will then try to look for the file in the same directory as the calling script. In this case that is nav-links.php, because PHP knows that nav-links.php was included into the index.php. So it will look in the functions directory where nav-links.php is located. Since there is a gen-list.php in that directory, PHP will include the gen-list.php file it finds there.
If you moved gen-list.php to the another directory, then the included would fail. Also, if you had another file called gen-list.php in same directory as index.php, then that file would be included and not the one in the functions directory. For more info about the include function check out the PHP manual.
I'm not entirely sure why:
<?php include("../functions/gen_list.php"): ?> (Relative to nav-links.php)
doesn't work. I maybe that you are simply confuse PHP. With the initial include search you are telling PHP to go up a directory from the index.php and find a functions directory that has a file call gen-links.php. That doesn't exist. The you're telling the fall back search to go one up from the functions directory, and then find the functions directory?
Joey L.
4,601 PointsJoey L.
4,601 PointsAppreciate it brother. It's all clear now!