37

How do I find out the filename of the script that called my function?

For example,

function sthing() {
echo __FILE__; // echoes myself
echo __CALLER_FILE__; // echoes the file that called me
}
Tower
  • 98,741
  • 129
  • 357
  • 507
  • Could you please give more info about why and to what purpose you would need such functionality? Might be that you are approaching a problem from the wrong end. – Gordon Dec 20 '09 at 14:22
  • To give an example: require with relative paths loads files from the root dir, even if you invoke require in a subdirectory. Having to prepend __DIR__ to every require call could be hidden away with a wrapper function, but then __DIR__ would point to the wrapper function's directory, not the callee's. – Lajos Mészáros Feb 24 '20 at 12:53

6 Answers6

46

A solution might be to use the debug_backtrace function : in the backtrace, that kind of information should be present.

Or, as Gordon pointed out in a comment, you can also use debug_print_backtrace if you just want to output that information and not work with it.


For instance, with temp.php containing this :

<?php
include 'temp-2.php';
my_function();

and with temp-2.php containing this :

<?php
function my_function() {
    var_dump(debug_backtrace());
}


Calling temp.php (i.e. the first script) from my browser gets me this output :

array
  0 => 
    array
      'file' => string '/.../temp/temp.php' (length=46)
      'line' => int 5
      'function' => string 'my_function' (length=11)
      'args' => 
        array
          empty

In there, I have the "temp.php" filename -- which is the one in which the function has been called.


Of course, you'll have to test a bit more (especially in situations where the function is not in the "first level" included file, but in a file included by another one -- not sure debug_backtrace will help much, there...) ; but this might help you get a first idea...

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

Try this code:

$key = array_search(__FUNCTION__, array_column(debug_backtrace(), 'function'));
var_dump(debug_backtrace()[$key]['file']);
Sunil Kumar
  • 6,112
  • 6
  • 36
  • 40
Mirko Pagliai
  • 1,220
  • 1
  • 19
  • 36
3

In addition to Pascal Martins's suggestion, you could install the PECL extension APD and use something like apd_callstack(), which (quoting example)

// returns an array containing an array of arrays.

Each array appears to contain:
[0] = function name
[1] = filename that contains function
[2] = *calling* line number in *calling* file
[3] = An array which is usually empty

But since this is a PECL extension and might interfere with Zend Optimizer, you might be better off going with debug_backtrace().

Gordon
  • 312,688
  • 75
  • 539
  • 559
2

2 lines - done:

$backfiles=debug_backtrace();
echo $file_called_from=$backfiles[0]['file']; // complete filepath

Or to crop out only the filename add the following

echo "<Br>";
echo basename($file_called_from); // for only the filename without the path
Jay Lepore
  • 85
  • 1
  • 7
2

This prints out file_name:line

function myFunction() {
  $backfiles = debug_backtrace();
  echo $backfiles[0]['file'] . ':' . $backfiles[0]['line'];
}
0

You can pass the filename as a parameter:

function sthing($filename) {
  echo __FILE__; // echoes myself
  echo $filename; // echoes the file that called me
}

When you call the function you pass the magic constant FILE:

sthing(__FILE__);