How can to get dir of file include / require current file
Moderator: General Moderators
How can to get dir of file include / require current file
ex :
demo/a.php
test/c.php
a.php require test/c.php
in c.php how can get dir of file a.php ?
demo/a.php
test/c.php
a.php require test/c.php
in c.php how can get dir of file a.php ?
- 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
Maybe this is what you are looking for :
Code: Select all
getcwd()Re: How can to get dir of file include / require current file
thk for help.novice4eva wrote:Maybe this is what you are looking for :Code: Select all
getcwd()
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
- 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
Code: Select all
require '../demo/a.php';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
thk all for help but i want get dir of file require / include. any function do this ?Jcart wrote:Code: Select all
require '../demo/a.php';
You may also be interested in $_SERVER['DOCUMENT_ROOT'] for specifying webroot path.
and about this viewtopic.php?f=1&t=92512
- 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
I don't understand what you are asking?
Get a directory of an include file? What does that mean?
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
Take a look at __FILE__ and __DIR__
Re: How can to get dir of file include / require current file
see ex:Jcart wrote:I don't understand what you are asking?
Get a directory of an include file? What does that mean?
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 ?
i usually dirname(__FILE__) get current dir of file working. But i can't get dir of file included file working.Mark Baker wrote:Take a look at __FILE__ and __DIR__
Re: How can to get dir of file include / require current file
Code: Select all
dirname($_SERVER["PHP_SELF"])- 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
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:
NOw in your file3.php you want
Is that possible!!
OK maybeeee, if i do
Code: Select all
//CONTENT of file1.php
require("folder2/file2.php");
.....
Code: Select all
//CONTENT of file2.php
require("folder3/file3.php");
....
Code: Select all
//CONTENT of file3.php
get me the folder of the file that will include me????
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
i think this will return dir of a.php but i want dir of file included c.php. In ex here is testbSyntac wrote:Although this would probably return a.php.Code: Select all
dirname($_SERVER["PHP_SELF"])
thk for your answer but i don't want set variable. i looking for built in function php to solve this.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"); .....NOw in your file3.php you wantCode: Select all
//CONTENT of file2.php require("folder3/file3.php"); ....
Is that possible!!Code: Select all
//CONTENT of file3.php get me the folder of the file that will include me????
OK maybeeee, if i doCode: 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;
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;
-
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
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
thk for your answer, but it wrong if i include some files before include file echo whereHaveIComeFrom($fileName)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; }
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
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;
}