How can to get dir of file include / require current file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hitmanmx
Forum Newbie
Posts: 13
Joined: Mon Dec 22, 2008 1:29 am

How can to get dir of file include / require current file

Post by hitmanmx »

ex :
demo/a.php
test/c.php

a.php require test/c.php
in c.php how can get dir of file a.php ?
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: How can to get dir of file include / require current file

Post by novice4eva »

Maybe this is what you are looking for :

Code: Select all

getcwd()
hitmanmx
Forum Newbie
Posts: 13
Joined: Mon Dec 22, 2008 1:29 am

Re: How can to get dir of file include / require current file

Post by hitmanmx »

novice4eva wrote:Maybe this is what you are looking for :

Code: Select all

getcwd()
thk for help.
i test but it gets the current working directory
ex :
testa/a.php
testb/b.php
testc/c.php

a.php require testb/b.php
b.php require testc/c.php
c.php still gets the current working directory testa not dir (testb) of file (b.php) require / include file c.php
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: How can to get dir of file include / require current file

Post by John Cartwright »

Code: Select all

require '../demo/a.php';
:?:

You may also be interested in $_SERVER['DOCUMENT_ROOT'] for specifying webroot path.
hitmanmx
Forum Newbie
Posts: 13
Joined: Mon Dec 22, 2008 1:29 am

Re: How can to get dir of file include / require current file

Post by hitmanmx »

Jcart wrote:

Code: Select all

require '../demo/a.php';
:?:

You may also be interested in $_SERVER['DOCUMENT_ROOT'] for specifying webroot path.
thk all for help but i want get dir of file require / include. any function do this ?
and about this viewtopic.php?f=1&t=92512
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: How can to get dir of file include / require current file

Post by John Cartwright »

I don't understand what you are asking?

Get a directory of an include file? What does that mean?
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: How can to get dir of file include / require current file

Post by Mark Baker »

Take a look at __FILE__ and __DIR__
hitmanmx
Forum Newbie
Posts: 13
Joined: Mon Dec 22, 2008 1:29 am

Re: How can to get dir of file include / require current file

Post by hitmanmx »

Jcart wrote:I don't understand what you are asking?

Get a directory of an include file? What does that mean?
see ex:
testa/a.php
testb/b.php
testc/c.php

a.php require testb/b.php
b.php require testc/c.php
In c.php how can get dir of file include (b.php). have any function ?
Mark Baker wrote:Take a look at __FILE__ and __DIR__
i usually dirname(__FILE__) get current dir of file working. But i can't get dir of file included file working.
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: How can to get dir of file include / require current file

Post by Syntac »

Code: Select all

dirname($_SERVER["PHP_SELF"])
Although this would probably return a.php.
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: How can to get dir of file include / require current file

Post by novice4eva »

OK if i am getting you correct, you have three files in three different folder, say folder1, folder2,folder2 and in each of those folders you have say file1.php, file2.php, file3.php. Now what you have in file is ..something like this:

Code: Select all

 
//CONTENT of file1.php
require("folder2/file2.php");
.....
 

Code: Select all

 
//CONTENT of file2.php
require("folder3/file3.php");
....
 
NOw in your file3.php you want

Code: Select all

 
//CONTENT of file3.php
get me the folder of the file that will include me????
 
Is that possible!!
OK maybeeee, if i do

Code: Select all

 
//CONTENT of file1.php
require("folder2/file2.php");
$PARENT_DIR_NAME = isset($PARENT_DIR_NAME)?$PARENT_DIR_NAME:dirname("folder2/file2.php");
.....
 

Code: Select all

 
//CONTENT of file2.php
require("folder3/file3.php");
$PARENT_DIR_NAME = isset($PARENT_DIR_NAME)?$PARENT_DIR_NAME:dirname("folder2/file2.php");
....
 

Code: Select all

 
//CONTENT of file3.php
echo $PARENT_DIR_NAME;
 
hitmanmx
Forum Newbie
Posts: 13
Joined: Mon Dec 22, 2008 1:29 am

Re: How can to get dir of file include / require current file

Post by hitmanmx »

Syntac wrote:

Code: Select all

dirname($_SERVER["PHP_SELF"])
Although this would probably return a.php.
i think this will return dir of a.php but i want dir of file included c.php. In ex here is testb
novice4eva wrote:OK if i am getting you correct, you have three files in three different folder, say folder1, folder2,folder2 and in each of those folders you have say file1.php, file2.php, file3.php. Now what you have in file is ..something like this:

Code: Select all

 
//CONTENT of file1.php
require("folder2/file2.php");
.....
 

Code: Select all

 
//CONTENT of file2.php
require("folder3/file3.php");
....
 
NOw in your file3.php you want

Code: Select all

 
//CONTENT of file3.php
get me the folder of the file that will include me????
 
Is that possible!!
OK maybeeee, if i do

Code: Select all

 
//CONTENT of file1.php
require("folder2/file2.php");
$PARENT_DIR_NAME = isset($PARENT_DIR_NAME)?$PARENT_DIR_NAME:dirname("folder2/file2.php");
.....
 

Code: Select all

 
//CONTENT of file2.php
require("folder3/file3.php");
$PARENT_DIR_NAME = isset($PARENT_DIR_NAME)?$PARENT_DIR_NAME:dirname("folder2/file2.php");
....
 

Code: Select all

 
//CONTENT of file3.php
echo $PARENT_DIR_NAME;
 
thk for your answer but i don't want set variable. i looking for built in function php to solve this.
if use var maybe you wrong. if i do

Code: Select all

 
//CONTENT of file1.php
require("folder2/file2.php");
$PARENT_DIR_NAME = dirname(__FILE__);
.....
 

Code: Select all

 
//CONTENT of file2.php
require("folder3/file3.php");
$PARENT_DIR_NAME = dirname(__FILE__);
....
 

Code: Select all

 
//CONTENT of file3.php
echo $PARENT_DIR_NAME;
 
Have any built in function php to solve this ?
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: How can to get dir of file include / require current file

Post by Mark Baker »

hitmanmx wrote:Have any built in function php to solve this ?

Code: Select all

 
function whereHaveIComeFrom($fileName) {
   $includeFiles = get_included_files();
   foreach($includeFiles as $includeFile) {
      if (pathinfo($includeFile,PATHINFO_BASENAME) == $fileName) {
         pathinfo($includeFile,PATHINFO_DIRNAME);
      }
   }
   return False;
}
 
hitmanmx
Forum Newbie
Posts: 13
Joined: Mon Dec 22, 2008 1:29 am

Re: How can to get dir of file include / require current file

Post by hitmanmx »

Mark Baker wrote:
hitmanmx wrote:Have any built in function php to solve this ?

Code: Select all

 
function whereHaveIComeFrom($fileName) {
   $includeFiles = get_included_files();
   foreach($includeFiles as $includeFile) {
      if (pathinfo($includeFile,PATHINFO_BASENAME) == $fileName) {
         pathinfo($includeFile,PATHINFO_DIRNAME);
      }
   }
   return False;
}
 
thk for your answer, but it wrong if i include some files before include file echo whereHaveIComeFrom($fileName)
ex :
testa/a.php require x.php require b.php
testa/x.php
testb/b.php echo whereHaveIComeFrom wrong (x.php) require c.php
testc/c.php echo whereHaveIComeFrom correct (b.php) because it last file require
it still get last file require does't get dir of file included / required current file
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: How can to get dir of file include / require current file

Post by Mark Baker »

So I forgot a return.

Code: Select all

 
function whereHaveIComeFrom($fileName) {
   $includeFiles = get_included_files();
   foreach($includeFiles as $includeFile) {
      if (pathinfo($includeFile,PATHINFO_BASENAME) == $fileName) {
         return pathinfo($includeFile,PATHINFO_DIRNAME);
      }
   }
   return False;
}
 
Post Reply