benzittlau Posted October 6, 2009 Share Posted October 6, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/176712-relative-path-not-behaving-as-expected/ Share on other sites More sharing options...
cags Posted October 6, 2009 Share Posted October 6, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/176712-relative-path-not-behaving-as-expected/#findComment-931678 Share on other sites More sharing options...
benzittlau Posted October 6, 2009 Author Share Posted October 6, 2009 Thanks for the help! I tried this in my code and it's now behaving as expected. This behavior seems strange; does anyone know if this is the intended behavior and/or why it was implemented as such? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/176712-relative-path-not-behaving-as-expected/#findComment-931682 Share on other sites More sharing options...
cags Posted October 6, 2009 Share Posted October 6, 2009 It is the intended behvoir. Including a file is tantamount to copying the code contents from one file to the other, meaning that any paths should be relative to their 'new-found' position. At least thats my understanding. Quote Link to comment https://forums.phpfreaks.com/topic/176712-relative-path-not-behaving-as-expected/#findComment-931692 Share on other sites More sharing options...
.josh Posted October 6, 2009 Share Posted October 6, 2009 Yes, this is expected behavior. For php, including one file in the other is the equivalent of c/p'ing code to your main file, as if it were hardcoded in the script. Therefore it is relative to the main script being executed. Quote Link to comment https://forums.phpfreaks.com/topic/176712-relative-path-not-behaving-as-expected/#findComment-931697 Share on other sites More sharing options...
benzittlau Posted October 6, 2009 Author Share Posted October 6, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/176712-relative-path-not-behaving-as-expected/#findComment-931701 Share on other sites More sharing options...
.josh Posted October 6, 2009 Share Posted October 6, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/176712-relative-path-not-behaving-as-expected/#findComment-931706 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.