-1

I have a file which will be included in a parent, so-to-speak, file. I need to reliably determine the path of the parent file from within the child file.

My first thought was $_SERVER['SCRIPT_FILENAME'] but that can be easily manipulated from the parent file.

Next thing I thought of was getcwd() but that will not always work, apache always provides the directory of the executed script but if it is run from cli or cron that is not the case.

If I can not trust the parent file, what is a secure way of determining its location from inside the file that is to be included?

php_nub_qq
  • 15,199
  • 21
  • 74
  • 144

1 Answers1

2

Offhand, can't find anything that'll directly give you the "parent" path in an include chain, but you CAN use debug_backtrace():

./z.php

<?php
include('y/y.php');

./y/y.php

<?php
var_dump(debug_backtrace());

And then:

$ php z.php
array(1) {
  [0]=>
  array(3) {
    ["file"]=>
    string(44) "/home/marc/z/z.php"
    ["line"]=>
    int(3)
    ["function"]=>
    string(7) "include"
  }

}

Marc B
  • 356,200
  • 43
  • 426
  • 500