473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

can an included file find out where it's been included from?

suppose i have 3 files:

script.php
somedir/include.php
somedir/config.php

1. script.php includes include.php
2. include.php includes config.php, so the structure looks like a cascade

now if i include config.php (without path) from include.php the script
tries to include from the directory script.php is in, which makes sense but
is not what i want.

question: how can i find out which dir include.php has been included from?

micha
Jul 17 '05 #1
6 2456
I know you can do with the $_SERVER array to find this information out,
but after a while the resulting code gets a bit messy.

Specifically:
$_SERVER['SCRIPT_FILENAME']
$_SERVER['PATH_TRANSLATED']
$_SERVER['SCRIPT_NAME']

Also, you may want to look into _FILE_, as documented here...
http://us3.php.net/manual/en/languag...predefined.php

When you get a chance, take some time out and really look at your file
structure and intereactivity between directories. You may find it would
be cleaner/easier to restructure things a bit.

~D

Jul 17 '05 #2
micha wrote:
suppose i have 3 files:

script.php
somedir/include.php
somedir/config.php

1. script.php includes include.php
2. include.php includes config.php, so the structure looks like a cascade

now if i include config.php (without path) from include.php the script
tries to include from the directory script.php is in, which makes sense but
is not what i want.

question: how can i find out which dir include.php has been included from?

micha


Micha,

Use the full absolute path to the file

<? include ($_SERVER['DOCUMENT_ROOT'] . '/somedir/config.php'); ?>

$_SERVER['DOCUMENT_ROOT'] gives you the absolute path to the root
directory for your web site. Then just concatenate the path and
filename to the one you want included.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 17 '05 #3
Another possible option is to simply use get_included_files() as IIRC
it returns the full path.

Jul 17 '05 #5
micha <ch*********@web.de> wrote in
news:Xn*************************@204.153.244.170:
suppose i have 3 files:

script.php
somedir/include.php
somedir/config.php

1. script.php includes include.php
2. include.php includes config.php, so the structure looks like a
cascade

now if i include config.php (without path) from include.php the script
tries to include from the directory script.php is in, which makes
sense but is not what i want.

question: how can i find out which dir include.php has been included
from?

micha


thanks for that many responses.

in case somebody is interested, i tested them:

1. $_SERVER['SCRIPT_FILENAME'], $_SERVER['PATH_TRANSLATED'], $_SERVER
['SCRIPT_NAME'] don't work, delivers information about the parent script,
even if the they're called from the included script.

2. __FILE__ works a treat - that's what i'm going to use

3. get_included_files() works equally well, but delivers an array that
i'd have to process.

4. debug_backtrace() does not work. i'd have to call it before file
inclusion, so i doesn't deliver any information about the files

micha

--
Jul 17 '05 #6
micha wrote:
question: how can i find out which dir include.php has been included from?

<snip>

Question flagged for FAQ.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Jul 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: aa | last post by:
Is a PHP variable supposed to be seen in a .js file included into a .php file? I have a client side javascript code stored in a .js file which is included into a PHP file using <script...
1
by: boclair | last post by:
I have this statement to find the latest date that a row was inserted ________ $entry=mysql_query("SELECT contact FROM board ORDER BY contact DESC LIMIT 1")or die ("Query...
1
by: Jan Stenberg | last post by:
I have a situation where I open a database connection in main script. Then I start a transaction, have some queries, include an another php file within some queries and then commiting queries in...
18
by: John Smith | last post by:
Hi all What does the group think of the practise of including one header file from inside another? I have some legacy code where this has been done, and it creates a dependency on a module...
0
by: Jonathan Wilson | last post by:
Firstly, to get msvcrt.lib, install the .NET framework SDK. The version of msvcrt.lib included there is the exact same one as comes with Visual Studio ..NET 2003. There are some other things that...
6
by: Wescotte | last post by:
I'm having an issue where what should be global variables are not in scope and I'm confused as to why. In Case 1 the variables are not in scope for functions in File2 In Case 2 the varibales are...
4
by: Madmartigan | last post by:
Hello I have attempted the source code from Chapter 4 in Accelerated C++ page 70 but keep getting the above error. I'm not sure whether it is in the way I have saved the header files or because my...
1
by: Xinhao Zheng | last post by:
hi all, Like the subject my question is. And i got another question,i know use get_included_files can be used to list all the files included already,so if i want to read the source code(i...
3
by: luttkens | last post by:
I've got a script to access my database called mysql.class.php. So from every other script that is talking to the database, this file i included, like this for instance: <?php...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.