Relative path in including


my INLUDED FILE

I have the text above in http://dot.kr/x-test/myPath1/included.php

<?php
include "../myPath1/included.php";
?>

And I have the code above in http://dot.kr/x-test/myPath/including.php.

The two pages produces same result because the second page include the 1st page.

<?php
include "myPath/including.php" ;
?>

I have the 3rd code above in http://dot.kr/x-test/mother.php

The 3rd file successfully includes the 2nd file,
The 2nd IN the 3rd file fails to open the 1st file and says the quote below although http://dot.kr/x-test/myPath/including.php succeeds to open the 1st file.

Warning: include(…/myPath1/included.php) [function.include]: failed to open stream: No such file or directory x-test\myPath\including.php on line 1

What’s wrong with it?

“PHP include” doesn’t support double relative path ?

the relative path to the file in an include() must be relative to the location of the php file the include() is in.

if you still have problems, post the actual php code in each of your php files.

clicking any links to a php file will only display any html output from that php file and not the php code itself.

I have 3 fils.

The text below is all text and no code in the 1st file which is in http://http://dot.kr/x-test/myPath1/included.php .

my INLUDED FILE

The code below is all code in the 2nd file which is in http://dot.kr/x-test/myPath/including.php.

<?php
include "../myPath1/included.php";
?>

The code below is all code in the 3rd file which is in http://dot.kr/x-test/mother.php.

<?php
include "myPath/including.php" ;
?>

in the 3rd file, try changing

 
include "myPath/including.php" ;
 

to

 
include "[COLOR=red]./[/COLOR]myPath/including.php" ;
 

btw - I normally enclose the path in () but it shouldn’t matter really

I changed the 3rd file like the below and saved it as mother1.php.

<?php
include "[COLOR="Red"]./[/COLOR]myPath/including.php" ;
?>

As I click the mother1.php, it produces the same Warning above.

since included.php and including.php are in the same directory change

include "../myPath1/included.php";

to

 
include "included.php";


I am afraid I don’t understand the above.

Do you mean that I should change the code in the file “including.php” in the directory of “myPath” from include “…/myPath1/included.php”; to "include “included.php”;?

The file “included.php” is in the directory of myPath1, not in myPath.
They(including.php and included.php) are not in the same directory.

no, sorry

I didn’t notice the 1 in myPath1. I thought both files were in myPath.

ahhh, I’ve learnt something new myself now :weee:

I’ve never had to nest includes like you are doing in this exercise but it looks like the reference point for all the relative paths in the nested includes needs to be the folder of the top php file in the nest.

so the file path in the including.php include() needs to be relative to the directory mother.php is in.

How can I make it?

Which file do I need to change between mother.php in x-test/ and including.php in x-test/myPath ?

ok, I set up a little test and that’s how I found out how it works.

I did this:

root folder: test

index.php

 
<?php
          include './junk/including.php';
?>

folder: test/junk

including.php

 
<?php
         include('./junk1/included.php');
?>

folder: test/junk1

included.php

 
<?php
         echo 'hello world';
?>

when I point my browser to index.php, I get ‘hello world’ displayed in the browser.

Now, it works fine. Thank you.

glad you sorted it out in the end.

we both learnt something new on this one :slight_smile:

Glad to hear you have even a thing to learn from me.

a wise man that Confucius :slight_smile:

and we’re never too old to learn.