SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,207
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    relative path in including

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


    Code:
    <?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.

    Code:
    <?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 ?

  2. #2
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  3. #3
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,207
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kalon View Post
    if you still have problems, post the actual php code in each of your php files.
    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 .
    Code:
    my INLUDED FILE
    The code below is all code in the 2nd file which is in http://dot.kr/x-test/myPath/including.php.
    Code:
    <?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.
    Code:
    <?php
    include "myPath/including.php" ;
    ?>

  4. #4
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    in the 3rd file, try changing

    Code:
     
    include "myPath/including.php" ;
    to

    Code:
     
    include "./myPath/including.php" ;
    btw - I normally enclose the path in () but it shouldn't matter really

  5. #5
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,207
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kalon View Post
    in the 3rd file, try changing
    Code:
     
    include "./myPath/including.php" ;
    I changed the 3rd file like the below and saved it as mother1.php.
    Code:
    <?php
    include "./myPath/including.php" ;
    ?>
    As I click the mother1.php, it produces the same Warning above.

  6. #6
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    since included.php and including.php are in the same directory change

    Code:
    include "../myPath1/included.php";
    to

    Code:
     
    include "included.php";

  7. #7
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,207
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kalon View Post
    since included.php and including.php are in the same directory change

    Code:
    include "../myPath1/included.php";
    to

    Code:
     
    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.

  8. #8
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no, sorry

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

  9. #9
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ahhh, I've learnt something new myself now

    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.

  10. #10
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,207
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kalon View Post
    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 ?

  11. #11
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

    Code:
     
    <?php
              include './junk/including.php';
    ?>
    folder: test/junk

    including.php

    Code:
     
    <?php
             include('./junk1/included.php');
    ?>
    folder: test/junk1

    included.php

    Code:
     
    <?php
             echo 'hello world';
    ?>
    when I point my browser to index.php, I get 'hello world' displayed in the browser.

  12. #12
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,207
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kalon View Post
    Code:
     
    <?php
             include('./junk1/included.php');
    ?>
    Now, it works fine. Thank you.

  13. #13
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    glad you sorted it out in the end.


    we both learnt something new on this one

  14. #14
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,207
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kalon View Post
    we both learnt something new on this one
    Glad to hear you have even a thing to learn from me.


    Quote Originally Posted by Confucius
    1. Teaching is actually Learning.
    2. Learning becomes perfect by teaching.

  15. #15
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    a wise man that Confucius

    and we're never too old to learn.


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
  •