简体   繁体   English

从PHP中包含的文件中获取父文件的路径,我不需要文件夹路径,但是包含页面的完整路径

[英]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. 我有一个php文件,可以将其包含在不同的位置。 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. 使用$_SERVER['REQUEST_URI']返回页面本身的路径,而不是包含它的父文件。

Try: 尝试:

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

The full list can be found on the PHP website . 完整列表可在PHP网站上找到。

I use spl_autoload_register so files are loaded when I use an instance of the class. 我使用spl_autoload_register,因此在使用该类的实例时会加载文件。 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. 页面的名称是扩展名为.php的类的名称。 Here's the code for my autoload.php page: 这是我的autoload.php页面的代码:

<?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");
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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