kitensei
kitensei

Reputation: 2530

Get current file name after include in PHP

Let's pretend I have the following class.wrapper.php

// loop through events/ directory ($eventFile = filename of parsed file)
include $eventFile;

myevent.php

echo __FILE__;

myotherevent.php

echo __FILE__;

Both of the echo return the same thing: class.wrapper.php, is there a way to get the real file name ? not the one which included the code ?

The class.wrapper is part of an encrypted API which allows user to define custom php files in a given directory to invoke user defined code when a given event is fired in the main application, so I cannot edit the main class, I have to do this only by editing "events" files

Upvotes: 0

Views: 214

Answers (2)

Athipatla
Athipatla

Reputation: 412

Try the below function

function GetInclude($file, $params = array())
    {
        extract($params);
        include $file;
    }
    myevent.php
    <?php GetInclude('class.wrapper.php', array('includerFile' => __FILE__)); ?>
    myotherevent.php
    <?php GetInclude('class.wrapper.php', array('includerFile' => __FILE__)); ?>
    class.wrapper.php
    echo $includerFile;

Upvotes: 1

Anwar
Anwar

Reputation: 4246

If you want to get the real name, which is for your example the complete path to access this file, use the function realpath("yourfile.extension").

I let you with this short example :

Let's say I have a file named index.php. I want to display its complete path. Here is the code to do so :

<?php
    echo realpath("index.php");
?>

In my example, I saved my file in C:\xampp\htdocs\test\index.php. The result will be a display of this exact path.

Hope it helps.

Upvotes: 0

Related Questions