How to get file name from a path in PHP?
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():
$filename = basename(path, suffix);
Path is a required field. It specifies the path to check.
Suffix is a optional field. It specifies a file extension. If the filename has this file extension, the file extension will not show.
2. Using pathinfo():
Also we can use the function pathinfo() which creates an array with the parts of the path we want to use.
$filename = pathinfo(path);
when we want to access the file name we will use $filename[‘basename’].