18

parent.php:

require_once 'child.php';

child.php:

echo __FILE__;

It will show '.../child.php'

How can i get '.../parent.php'

Max Frai
  • 61,946
  • 78
  • 197
  • 306

7 Answers7

21

The chosen answer only works in environments that set server variables and specifically won’t work from a CLI script. Furthermore, it doesn't determine the parent, but only the topmost script file.

You can do almost the same thing from a CLI script by looking at $argv[0], but that doesn’t provide the full path.

The environment-independent solution uses debug_backtrace:

function get_topmost_script() {
  $backtrace = debug_backtrace(
      defined("DEBUG_BACKTRACE_IGNORE_ARGS")
      ? DEBUG_BACKTRACE_IGNORE_ARGS
      : FALSE);
  $top_frame = array_pop($backtrace);
  return $top_frame['file'];
}
danorton
  • 11,804
  • 7
  • 44
  • 52
  • 2
    Probably here must to be "array_shift" instead of "array_pop" – FDisk Feb 20 '13 at 09:31
  • Nope, using `array_shift()` there will cause the function to return the name of the script file that contains `get_topmost_script()`, i.e. `__FILE__`. – danorton Feb 23 '13 at 04:32
16
print $_SERVER["SCRIPT_FILENAME"];
Sampson
  • 265,109
  • 74
  • 539
  • 565
1

I don't think you can do that : the __FILE__ magic constant indicates in which file it is written ; and that is all.

If you want to know which PHP script was initially called (which URL was requested, for instance), you might have more luck looking at the $_SERVER superglobal : it contains many informations, including some that will help you (like SCRIPT_FILENAME or SCRIPT_NAME, for instance) ;-)

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
0

Straight to the solution:

Parent Script:

echo $_SERVER['SCRIPT_FILENAME'];

Child Script (included file in parent script):

echo __FILE__;

Make a file called "parent.php" with contents:

include('child.php');

Make a file called "child.php" with contents:

echo "My Parent is at: " . $_SERVER['SCRIPT_FILENAME'] . "<br>";
echo "I'm at: " .  __FILE__;

You get the idea...

Tarik
  • 4,270
  • 38
  • 35
0

You have to use basename($_SERVER["SCRIPT_FILENAME"]) or, if you like the script nam only, you can use basename($_SERVER["SCRIPT_FILENAME"], '.php').

Tunaki
  • 132,869
  • 46
  • 340
  • 423
0

The other way to do this (works under both Apache and CLI) is by using the get_included_files() function. It returns an array with all of the included files (including the initial calling script) in the order they were run.

get_included_files()[0] = full path of initial script.

E.g

/myscripts/Parent.php

<?php
   echo("Called from parent: ".get_included_files()[0]."\r\n");
   include("/otherscripts/Child.php");
   echo("Called again from parent: ".get_included_files()[0]."\r\n");
?>

/otherscripts/Child.php

<?php
   echo("Called from child: ".get_included_files()[0]."\r\n");
?>

Output:

> php /myscripts/Parent.php
Called from parent: /myscripts/Parent.php
Called from Child: /myscripts/Parent.php
Called again from parent: /myscripts/Parent.php

I eventually found the solution to this elsewhere on stackoverflow (Get absolute path of initially run script) while looking for something else so have modified and added to this question as this was where I landed first!

0

If you have layers of "include" files and want to get where the request came from, I found the easiest way is to have the requesting parent pass it's ID through:

parent script:

required_once('child.php');
childFunction(__FILE__);

child script:

function childFunction($source);
    echo 'Hi '.$source.', how can I help you?';
}

To make it more useful for debugging, best replace __FILE__ with a string which includes the line number, such as "parent.php L1234".

will
  • 153
  • 3
  • 6