Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get name of file that is including a PHP script

Tags:

Here is an example of what I am trying to do:

index.php

<ul><?php include("list.php") ?></ul> 

list.php

<?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.

like image 265
valon Avatar asked Jul 24 '11 02:07

valon


People also ask

How to get name of PHP file?

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

What PHP file contains?

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.

How to get the current file name or directory name in PHP?

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.

What is a computer file in PHP?

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

Where does PHP_self get its information from?

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.

What is $_server in PHP?

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.


2 Answers

$_SERVER["PHP_SELF"]; returns what you want

like image 145
Caner Avatar answered Sep 30 '22 01:09

Caner


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 } 
like image 24
zerkms Avatar answered Sep 30 '22 02:09

zerkms