0

First of all I would like apology if this question has already been answered, I'm searching for two days with no luck.

Here is my setup:

I have 2 php files, in 2 different folders:

AAA/1.php AAA/BBB/CCC/2.php

"2.php" is included in "1.php".

Since I need to use "2.php" in several different folders, I need it to be able to know in what folder he's localized. So in "2.php" I should have some code like:

    $directory = [whatever code I need to know where I am];

So in the above example, the relative path should be: BBB/CCC

I tried with several functions and coding I found but I can only get the full server path to "1.php".

Can anyone help me?

1

2 Answers 2

2

I believe what you are looking for is dirname(__FILE__);

See PHP: Magic Constants and dirname() for more info.

__FILE__ The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2, FILE always contains an absolute path with symlinks resolved whereas in older versions it contained relative path under some circumstances.

Sign up to request clarification or add additional context in comments.

Comments

0

You could use document root as start of your path so you never have to worry about it.

e.g.

$_SERVER['DOCUMENT_ROOT'].'/AAA/1.php'

and

$_SERVER['DOCUMENT_ROOT'].'/AAA/BBB/CCC/2.php'

should work from no matter where you are as long as your php.ini configuration doesn't change too much.

Or you could use combinations of basename(), dirname()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.