Get name of file that is including a PHP script

后端 未结 6 2007
遇见更好的自我
遇见更好的自我 2020-12-16 11:22

Here is an example of what I am trying to do:

index.php

list.php

6条回答
  •  自闭症患者
    2020-12-16 12:06

    If you really need to know what file the current one has been included from - this is the solution:

    $trace = debug_backtrace();
    
    $from_index = false;
    if (isset($trace[0])) {
        $file = basename($trace[0]['file']);
    
        if ($file == 'index.php') {
            $from_index = true;
        }
    }
    
    if ($from_index) {
        // Do something
    } else {
        // Do something else
    }
    

提交回复
热议问题