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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • micha

    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
  • dracolytch

    #2
    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_FILENAM E']
    $_SERVER['PATH_TRANSLATE D']
    $_SERVER['SCRIPT_NAME']

    Also, you may want to look into _FILE_, as documented here...


    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

    Comment

    • Jerry Stuckle

      #3
      Re: can an included file find out where it's been included from?

      micha wrote:[color=blue]
      > 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[/color]

      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@attgl obal.net
      =============== ===

      Comment

      • Chung Leong

        #4
        Re: can an included file find out where it's been included from?



        Comment

        • Philip  Olson

          #5
          Re: can an included file find out where it's been included from?

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

          Comment

          • micha

            #6
            Re: can an included file find out where it's been included from?

            micha <chotiwallah@we b.de> wrote in
            news:Xns96548D5 4C3F51chotiwall ah@204.153.244. 170:
            [color=blue]
            > 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[/color]

            thanks for that many responses.

            in case somebody is interested, i tested them:

            1. $_SERVER['SCRIPT_FILENAM E'], $_SERVER['PATH_TRANSLATE D'], $_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_fi les() 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

            --

            Comment

            • R. Rajesh Jeba Anbiah

              #7
              [Flag-FAQ] Re: can an included file find out where it's been included from?

              micha wrote:[color=blue][color=green]
              > > question: how can i find out which dir include.php has been[/color][/color]
              included[color=blue][color=green]
              > > from?[/color][/color]
              <snip>

              Question flagged for FAQ.

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

              Comment

              Working...