Page 1 of 1
How can to get dir of file include / require current file
Posted: Mon Dec 22, 2008 7:52 pm
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 ?
Re: How can to get dir of file include / require current file
Posted: Mon Dec 22, 2008 8:13 pm
by novice4eva
Maybe this is what you are looking for :
Re: How can to get dir of file include / require current file
Posted: Mon Dec 22, 2008 10:38 pm
by hitmanmx
novice4eva wrote:Maybe this is what you are looking for :
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
Re: How can to get dir of file include / require current file
Posted: Mon Dec 22, 2008 10:45 pm
by John Cartwright
You may also be interested in $_SERVER['DOCUMENT_ROOT'] for specifying webroot path.
Re: How can to get dir of file include / require current file
Posted: Tue Dec 23, 2008 12:53 am
by hitmanmx
Jcart wrote:
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
Re: How can to get dir of file include / require current file
Posted: Tue Dec 23, 2008 5:42 pm
by John Cartwright
I don't understand what you are asking?
Get a directory of an include file? What does that mean?
Re: How can to get dir of file include / require current file
Posted: Tue Dec 23, 2008 6:05 pm
by Mark Baker
Take a look at __FILE__ and __DIR__
Re: How can to get dir of file include / require current file
Posted: Tue Dec 23, 2008 7:43 pm
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.
Re: How can to get dir of file include / require current file
Posted: Tue Dec 23, 2008 10:43 pm
by Syntac
Although this would probably return a.php.
Re: How can to get dir of file include / require current file
Posted: Wed Dec 24, 2008 12:14 am
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;
Re: How can to get dir of file include / require current file
Posted: Wed Dec 24, 2008 12:59 pm
by hitmanmx
Syntac wrote:
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 ?
Re: How can to get dir of file include / require current file
Posted: Wed Dec 24, 2008 4:18 pm
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;
}
Re: How can to get dir of file include / require current file
Posted: Thu Dec 25, 2008 9:02 pm
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
Re: How can to get dir of file include / require current file
Posted: Fri Dec 26, 2008 4:49 am
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;
}