SQL PHP HTML ASP JavaScript articles and free scripts to download If you are facing any problem in viewing this page, please tell us
 
 

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"];
echo $file;


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.

/my_file/test.php

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.

$break = Explode('/', $file);
$pfile = $break[count($break) - 1];


Here $pfile is the variable which will have the value of present file name.

We can use $pfile in different application where current file name is required .

Here is the complete code.

$file = $_SERVER["SCRIPT_NAME"];
$break = Explode('/', $file);
$pfile = $break[count($break) - 1];

echo $pfile;

Further readings
PHP file handling functions
Reading from local and remote files by using fopen()
Writing to a file by fwrite() function
file_exists: Checking if file is present or not
unlink:Deleting file by using unlink function
Deleting all files present inside a directory
Downloading files using header control
File upload to server using PHP
File upload using PHP 5 and register_global off
Uploading More than one file to server
Displaying all files and directory of a folder
Getting the Present file name running the PHP script
Getting the last updated time of the file in PHP
Displaying the last modification time of the file
Searching for text within a pair of tags of a html page
Listing All title tags inside a directory
Creating thumbnail images after uploading the file















Sections
PHP
JavaScript
ASP
HTML
SQL
Photoshop
Articles SEO
PHP Tutorials
File handling
PHP Monthly Planner
PHP Introduction
Loops & structure
Array
Date & Time
Functions
Form Handling
File Handling
Math Functions
String Functions
GD Functions
Comment Posting
Content Management
PHP & Ajax
Popular Tutorials
Drop down list
File Upload
Signup script
Member Login
Line Graph
PHP MySQL Paging
PHP Calendar
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.