PHP School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a PHP Expert!

Join 300,406 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,654 people online right now. Registration is fast and FREE... Join Now!




Issues with require_once()

 

Issues with require_once(), relative paths problem

maffelu

2 Jul, 2009 - 01:44 PM
Post #1

D.I.C Head
Group Icon

Joined: 21 Aug, 2008
Posts: 134



Thanked: 9 times
My Contributions
Righ!

I have an issue that's been bothering me for some days.

I'm making a smaller class library with a certain hierarchy. These classes lies in separate directories in a dir called, say, MyLib.

/MyLib/rootClass.php

/MyLib/misc/someClass.php

Now. someClass.php requires the rootClass.php which should be done like this:

CODE

require_once('../rootClass.php');


This should work fine. Problem is when I use the 'someClass' from a file in a different directory. Then I can't require the file this way since the current dir is another dir.

Is there a way in PHP to always get the current dir of the file required? So that someClass.php always can reach files in the same dir hierarchy that it lies in itself?


User is offlineProfile CardPM
+Quote Post


AdaHacker

RE: Issues With Require_once()

2 Jul, 2009 - 02:24 PM
Post #2

D.I.C Regular
***

Joined: 17 Jun, 2008
Posts: 378



Thanked: 83 times
My Contributions
You're looking for the __FILE__ magic constant. That gives the full path to the currently executing file, which can easily give you the directory.
User is offlineProfile CardPM
+Quote Post

maffelu

RE: Issues With Require_once()

2 Jul, 2009 - 09:21 PM
Post #3

D.I.C Head
Group Icon

Joined: 21 Aug, 2008
Posts: 134



Thanked: 9 times
My Contributions
But __FILE__ gives that current dir, is it possible to edit that value to "this dir - 1" so to speak, so if the file lies here:

/root/foo/bar/myFile.php

and I want to reach

/root/foo/otherFile.php

thatn __FILE__ will only give me the /root/foo/bar dir, and I don't want to get that far. Is that possible in any way?
User is offlineProfile CardPM
+Quote Post

maffelu

RE: Issues With Require_once()

2 Jul, 2009 - 09:47 PM
Post #4

D.I.C Head
Group Icon

Joined: 21 Aug, 2008
Posts: 134



Thanked: 9 times
My Contributions
Actually it's __DIR__ I want.

Problem is still here though, check this out

someFile.php
CODE

require_once(__DIR__.'../otherFile.php'); //<--That's sort of what I want to do


someFile is in this dir:
root/foo/bar/someFile.php

and I want to reach this file
root/foo/otherFile.php

can __DIR__ move one step up in the dir hierarcy?


User is offlineProfile CardPM
+Quote Post

maffelu

RE: Issues With Require_once()

2 Jul, 2009 - 10:05 PM
Post #5

D.I.C Head
Group Icon

Joined: 21 Aug, 2008
Posts: 134



Thanked: 9 times
My Contributions
[SOLVED]


require_once(dirname(__FILE__).'/../someFile.php');
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 10:53PM

Live PHP Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month