In this post, you will know how to get filename without extension from given file path in PHP.
You will just need to use very simple PHP pre-defined methods to get filename.
- $imagePath = "{file-path}/filename.png";
- $ext = pathinfo($imagePath, PATHINFO_EXTENSION);
- $file = basename($imagePath,".".$ext);
- print_r($file);
In above example file-path will be path of your selected file.
pathinfo
method is used to get information about path.
PATHINFO_EXTENSION
is used to get extension of file if there are more than one extension with given path then it will return last one only.
basename
is used to get filename without extension. In basename method, second parameter suffix specifies a extension that is optional but if you need to get only filename then you will have to pass extension as second argument in this method.