Jump to content

Recommended Posts

I am developing some code which is in a folder hierarchy, which I will call:

/first/second/third/fourth

 

I have a class located in a php file c.php which is located in folder "third".  There is a dependency for this class called b.php located in folder "second".  I am trying to use require_once to load the file b.php using the following code:

 

require_once '../b.php';

 

This works correctly if I directly load the file c.php, but it does *not* work when the file c.php is included included from another file.  For example I have some code in a file a.php located in the folder "first".  If I try and include the file c.php, I receive an error that it can't find the file b.php. 

 

Through some blood sweat and tears I've discovered that when I am including file c.php into a.php, the relative path that is being used to find the file b.php is the path of the file a.php, not the path of c.php!  This only occurs when the relative path starts with a "../".  If I am trying to include a file in c.php which belongs in the folder "fourth" by using:

 

require_once 'fourth/d.php';

 

I don't have this problem.  Is this expected behaviour for php?  Is there a better method I should be using for these relative references?

 

FYI, I am using PHP 5.3.0-0.

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/176712-relative-path-not-behaving-as-expected/
Share on other sites

I struggled with this issue for months untill I came across this...

 

dirname(__FILE__) . '/../scripts/settings.php';

 

... simply use (what I class as the logical) relative path like you were before. It basically ensures the path is relative to the original script not the file it's included in.

That was my initial belief as well, but then why does it only change the relative paths of references beginning with "../"?

 

As I mentioned in my first post if I include a file d.php in the folder "fourth" in c.php, it still works correctly when c.php is included into a.php:

 

require_once 'fourth/d.php';

 

Folder hierachy for reference:

 

first

  a.php

  second

    b.php

    third

      c.php

    fourth

        d.php

because your working script is c.php so "fourth/d.php" is indeed relative to it.

 

edit: ah wait, a.php is your working script.  But nonetheless, "../" specifically means "the directory above the working directory" so it will always be relative to the working script.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.