简体   繁体   中英

Get path of parent file from included file in php, I don't need the folder path but the full path of including page

I have a php file that could be included in different places. I need to have some rule on what to show depending on the including file.

Using $_SERVER['REQUEST_URI'] is returning the path of the page itself not the parent file that's including it.

Try:

<?php
echo $_SERVER['SCRIPT_FILENAME'];

The full list can be found on the PHP website .

I use spl_autoload_register so files are loaded when I use an instance of the class. I put all my global functions as public static functions of classes. I only have one include in my pages:

require "autoload.php";

The name of the page is the name of the class with a .php extension. Here's the code for my autoload.php page:

<?php
function my_autoload($sClassName)
{
    // LOAD THE CLASS.
    for($loop=0;$loop < strlen($sClassName);$loop++)
    {
        $thischar = substr($sClassName,$loop,1);
        if (($thischar != "_") && ((!ctype_alnum($thischar)) || (ctype_digit($thischar))))
        {
            // INVALID CLASS NAME.
            echo 'Class ' . $sClassName . ' has invalid characters.';
            return;
        }
    }
    $filename = dirname(__FILE__) . '/' . strtolower($sClassName) . '.php'; 
    if (file_exists($filename))
    {
        require($filename);
    } else
    {
        echo 'Error: file ' . $filename . ' not found looking for class ' . $sClassName;
    }
    return;
}
spl_autoload_register("my_autoload");
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM