Given full path, find file name in the path.
Input : path = /testweb/var/www/mywebsite/htdocs/home.php
Output : home.php
Input : path = /testweb/var/www/mywebsite/htdocs/abc.txt
Output : abc.txt
1. Using basename() :
Syntax:
$filename = basename(path, suffix);
Example:
<!--PHP code to get file name--> <!--from path name--> <?php $path = "/testweb/var/www/mywebsite/htdocs/home.php"; $file1 = basename($path); $file2 = basename($path, ".php"); //Show filename with file extension echo $file1 . "\n"; //Show filename without file extension echo $file2; ?>
2.Using Pathinfo() :
Also we can use the function pathinfo() which creates an array with the parts of the path we want to use.
Syntax:
$filename = pathinfo(path);
Example 2:
<!--PHP code to get file name--> <!--from path name--> <?php //Path of the file stored under pathinfo $myFile = pathinfo('/usr/admin/config/test.php'); //Show the file name echo $myFile['basename'], "\n"; ?>