How to get filename without extension in PHP

How to get filename without extension in PHP

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.

  1. $imagePath = "{file-path}/filename.png";
  2. $ext = pathinfo($imagePath, PATHINFO_EXTENSION);
  3. $file = basename($imagePath,".".$ext);
  4. 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.

Phone: (+91) 8800417876
Noida, 201301