3

i have the following PHP File Structure:

dir0
    dirA    
        dirA.1    
            dirA.1.1    
                fileA.php : dirname(__FILE__).'/docs';    
        dirA.2
        ...    
    dirB    
       fileB.php: include_once("../dirA/dirA.1/dirA.1.1/fileA.php");    
    dirC    
       fileC.php: include_once("../dirA/dirA.1/dirA.1.1/fileA.php");

in the included fileA.php i use dirname(__FILE__).

i get the currect path if i call the function of fileA.php directly. BUT i get the wrong path if i call it (after include) in fileB.php and fileC.php, ...etc!

how can i get the directory of the included file?

NOTE: the name of dir0(root) may change, so i don't want to use static Strings for these paths!

Rami.Q
  • 2,486
  • 2
  • 19
  • 30
  • So in fileA you define a function which you call in FileB. And upon calling it in fileB you want the directory of FileA right? And you include your FileA in FileB or somewhere else? – Andresch Serj Apr 16 '14 at 10:06
  • yes, and fileA is included in many files like FileB, FileC, ...etc. fileA contains a "universal function", in this function i need its directory path (dir0/dirA/dir1.1/dirA.1.1/docs) – Rami.Q Apr 16 '14 at 10:10
  • If FileA has a definition of a function, it should only be included once. Uppon inclusion, you do know the path (*otherwise you can not include it right?*). So store that Path uppon inclusion and use it. – Andresch Serj Apr 16 '14 at 10:26
  • @AndreschSerj: its not a bad idea, but it would be better, not to do this uppon every inclusion, it could be better if there is a way just to change that part of the inner function of fileA! my current workarround is to define a _GLOBAL VAR_ at the top of fileA, and i use in the inner function **global $dirname**; – Rami.Q Apr 16 '14 at 11:21
  • as i said, it should not be included serveral times if it defines function since it would only redefine them which is ... well ... bad. Consider rethinking your architecture. Maybe read up on Dependency Injection and Configuration. – Andresch Serj Apr 16 '14 at 11:28
  • @AndreschSerj, im not sure if i understand you correctly or not, but i have to include it at the top of every link i have. for ex.: home, products, ...etc! – Rami.Q Apr 16 '14 at 11:36
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/50780/discussion-between-andresch-serj-and-rami-q) – Andresch Serj Apr 16 '14 at 11:38
  • if you have in your fileA a `function mypath() { return dirname(__FILE__); }` , if you then include the fileA in another fileB and in fileB you do `echo mypath();` it will output the path of fileA. Whats the problem? – Sharky Apr 16 '14 at 11:45
  • @Sharky, no it will output the path of fileB! – Rami.Q Apr 16 '14 at 18:25

2 Answers2

3

You don't get the wrong path.

An include makes the called file part of the script it has been called from, but it won't change the path PHP is pointing to. So if you call fileA from dirB, PHP is still in dirB.

But you know dirA (otherwise you can't include fileA), so chdir("../dirA/dirA.1/dirA.1.1/") will point PHP to dirA, even from fileB or file C.

You could consider working from dir0. Call fileB with dirB/fileB.php and include fileA with include "dirA/dirA.1/dirA.1.1/fileA.php". In that case you always know where you are in the tree: in dir0.

Edit I was wrong. If you call dirname(__FILE__); from the included fileA, it returns the path of fileA. So all you have to do at the top of fileA is:

$mypath=dirname(__FILE__);
function myfunction($path){
    echo $path;
    }

And in fileB call your function while passing $mypath:

 myfunction($mypath);

Note that you might have to do some fiddling with slashes and backslashes, depending on the system your server is running.

Michel
  • 4,076
  • 4
  • 34
  • 52
  • thanks for your answer. i know that PHP is still in dirB, and thats my problem. But do you mean i have to change dir every time i call that function? omg. this is not a good idea – Rami.Q Apr 16 '14 at 10:20
0

This is a good candidate for get_included_files(). This function will return a numerically indexed array containing all the files that are included. "Gets the names of all files that have been included using include, include_once, require or require_once."
https://www.php.net/manual/en/function.get-included-files.php

You can get the directories that have included files like this:

array_map("dirname",get_included_files());