How to get the current file name using PHP script ? Last Updated : 16 Feb, 2021 Comments Improve Suggest changes Like Article Like Report In this article, we will learn how to get the current filename using PHP. Input : c:/xampp/htdocs/project/home.php Output : project/home.php Input : c:/xampp/htdocs/project/abc.txt Output : project/abc.txt Sometimes, we need to get the current file name with the directory name in which our page is stored for different purposes. 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. There is no other way that every web server will provide any of this information. Syntax: 'SCRIPT_NAME' gives the path from the root to include the name of the directory. To get the current file name. We use $currentPage= $_SERVER['SCRIPT_NAME']; To show the current file name. We use echo $currentPage; PHP code: The following is the complete code to get the current file name with the directory name. PHP <?php // To Get the Current Filename. $currentPage= $_SERVER['SCRIPT_NAME']; // To Get the directory name in // which file is stored. $currentPage = substr($currentPage, 1); // To Show the Current Filename on Page. echo $currentPage; ?> Output: project/home.php In this code, substr() PHP function is used to extract the part of string from $currentPage variable string. Comment More infoAdvertise with us Next Article How to get the current file name using PHP script ? I iamjatinkumar Follow Improve Article Tags : Web Technologies PHP PHP Programs PHP-Questions Similar Reads How to get the file size using PHP ? In this article, we are going to discuss how to get the file size using PHP. PHP is a general-purpose scripting language that is suited for both server and client scripting language for website development. To get the file size, we will use filesize() function. The filesize() function returns the s 1 min read How to post data using file_get_contents in PHP ? The file_get_contents() function in PHP is used to read the contents of a file and make HTTP requests using GET and get HTTP responses using POST methods. The HTTP POST request can be made using the $context parameter of the file_get_contents() function, which posts the specified data to the URL spe 3 min read How to move a file into a different folder on the server using PHP? The move_uploaded_file() function and rename() function is used to move a file into a different folder on the server. In this case, we have a file already uploaded in the temp directory of server from where the new directory is assigned by the method. The file temp is fully moved to a new location. 3 min read How to get names of all the subfolders and files present in a directory using PHP? Given the path of the folder and the task is to print the names of subfolders and files present inside them. Explanation: In our PHP code, initially, it is checked whether provided path or filename is a directory or not using the is_dir() function. Now, we open the directory using opendir() function 2 min read How to delete a file using PHP ? To delete a file by using PHP is very easy. Deleting a file means completely erase a file from a directory so that the file is no longer exist. PHP has an unlink() function that allows to delete a file. The PHP unlink() function takes two parameters $filename and $context. Syntax: unlink( $filename, 2 min read Like