Find the calling script path from the library

When executing a script that includes the library, I want to find the library of the calling script from the library, in Perl I use env:, $0which gives me the path to the calling script, in PHP __FILE__, the current script is given, so in the library it gives me the path to the library , not the path of the calling script.

from perl documentation: $ 0 Contains the name of the file containing the Perl script to be executed.

I think this can be done with debug_backtrace (), but is there another better / shorter method?

EDIT: (added sample code)

file: index.php

<?php
require 'locallib.php';
echo 'in original script = '.__FILE__.'<br />';
?>

file: locallib.php

<?php
require "lib.php";
echo 'in library "'.__FILE__.'"<br />';
?>

file: lib.php

<?php
if( $_SERVER['SCRIPT_FILENAME'] != '/var/www/html/index.php')
{
    echo "Not allowed";exit;
} else 
{
    echo 'in library "'.__FILE__.'"<br />';
    echo '<pre>';
    print_r($_SERVER);
    echo '</pre>';
}
?>
+3
source share
3 answers
$_SERVER['SCRIPT_FILENAME'];

$_SERVER .

+3

$_SERVER PHP script .

, , -, , :

function php_self(){
  $bt = debug_backtrace();
  return $bt[count($bt)-1]['file'];
}
+6

Have you tried this:

$_SERVER['SCRIPT_FILENAME'];
$_SERVER['PHP_SELF'];
+1
source

Source: https://habr.com/ru/post/1757670/


All Articles