How to get filename without extension in PHP

How to get filename without extension in PHP

In this post we will give you information about How to get filename without extension in PHP. Hear we will give you detail about How to get filename without extension in PHPAnd how to use it also give you demo for it if it is necessary.

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);
$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.

$ext = pathinfo($imagePath, PATHINFO_EXTENSION);

This was a shortest way to get filename extension with PHP.

Hope this code and post will helped you for implement How to get filename without extension in PHP. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

8  +  1  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US