How to get the current script running name of the file in PHP
We can get the current file name or the file executing the code by using SCRIPT_NAME. This will give us the path from the server root so the name of the current directory will also be included. Here is the code.
$file = $_SERVER["SCRIPT_NAME"];
The above lines will print the present file name along with the directory name. For example if our current file is test.php and it is running inside my_file directory then the output of above code will be.
We will add some more code to the above line to get the only file name from the above code. We will use explode command to break the string by using delimiter “/” .
As the output of this explode command is an array then we will collect the last element of this array to get our file name. Here the index of last element of the array is total element of the array minus one, because the index of the elements start from 0 ( not from one ).
So index of the last element of the array = total number of elements – 1
Here is the code to get the last element of the array with the explode command to get the array.