Here is an example of what I am trying to do:
<ul><?php include("list.php") ?></ul>
<?php if (PAGE_NAME is index.php) { //Do something } else { //Do something } ?>
How can I get the name of the file that is including the list.php script (PAGE_NAME)? I have tried basename(__FILE__)
, but that gives me list.php
.
The basename() function is an inbuilt function that returns the base name of a file if the path of the file is provided as a parameter to the basename() function. Syntax: $filename = basename(path, suffix);
Generally speaking, a PHP file is a plain-text file which contains code written in the PHP programming language. Since PHP is a server-side (back-end) scripting language, the code written in the PHP file is executed on the server.
Since we are using PHP, we can easily get the current file name or directory name of the current page by using $_SERVER [‘SCRIPT_NAME’]. Using $_SERVER [‘SCRIPT_NAME’]: $_SERVER is an array of stored information such as headers, paths, and script locations. These entries are created by the webserver.
A computer file is a computer resource on a computer that stores data, information, picture, video, settings, or commands used with a computer program. In a graphical user interface an operating system displays a file as an icon. <?php $current_file_name = basename ($_SERVER ['PHP_SELF']); echo $current_file_name." "; ?>
The webserver creates all this information. PHP_SELF is a variable used to get the filename of the currently executing script. It is relative to the document root. When the user runs this command in the command line, it will print the information about the script name.
Using $_SERVER [‘SCRIPT_NAME’]: $_SERVER is an array of stored information such as headers, paths, and script locations. These entries are created by the webserver.
$_SERVER["PHP_SELF"];
returns what you want
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 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With