![]() |
VOOZH | about |
In this article, we will learn how to get the current file extensions in PHP.
Input : c:/xampp/htdocs/project/home Output : "" Input : c:/xampp/htdocs/project/index.php Output : ".php" Input : c:/xampp/htdocs/project/style.min.css Output : ".css"
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:
$_SERVER[‘SCRIPT_NAME’]
Method 1: The following method uses the strpos() and substr() methods to print the values of the last occurrences.
php
Method 2: The following method uses a predefined function pathinfo(). In the output, the "Name:" shows the name of the file and "Extension:" shows the file extension.
PHP code:
Name: 001510d47316b41e63f337e33f4aaea4 Extension: php
Method 3: The following code uses the predefined function parse_url() and pathinfo() for URLs.
PHP code:
Name: file.index Extension: php