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

This is a discussion on can an included file find out where it's been included from? within the PHP Language forums, part of the PHP Programming Forums category; suppose i have 3 files: script.php somedir/include.php somedir/config.php 1. script.php includes include.php 2. ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-12-2005
micha
 
Posts: n/a
Default 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
Reply With Quote
  #2 (permalink)  
Old 05-12-2005
dracolytch
 
Posts: n/a
Default Re: can an included file find out where it's been included from?

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

Reply With Quote
  #3 (permalink)  
Old 05-12-2005
Jerry Stuckle
 
Posts: n/a
Default Re: can an included file find out where it's been included from?

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.
jstucklex@attglobal.net
==================
Reply With Quote
  #4 (permalink)  
Old 05-12-2005
Chung Leong
 
Posts: n/a
Default Re: can an included file find out where it's been included from?

http://us3.php.net/debug_backtrace

Reply With Quote
  #5 (permalink)  
Old 05-12-2005
Philip Olson
 
Posts: n/a
Default Re: can an included file find out where it's been included from?

Another possible option is to simply use get_included_files() as IIRC
it returns the full path.

Reply With Quote
  #6 (permalink)  
Old 05-13-2005
micha
 
Posts: n/a
Default Re: can an included file find out where it's been included from?

micha <chotiwallah@web.de> wrote in
news:Xns96548D54C3F51chotiwallah@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

--
Reply With Quote
  #7 (permalink)  
Old 05-15-2005
R. Rajesh Jeba Anbiah
 
Posts: n/a
Default [Flag-FAQ] Re: can an included file find out where it's been included from?

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/

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 11:27 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0