Getting filename and extension in PHP using explode() ,basename() and pathinfo()
Posted on January 22, 2008
Filed Under php, tips and technique
Getting the extension from a file name or name of the file from the full path is not a big hassle for the experienced programmer but it might be a annoying task for the beginners of PHP. Now let me find the solution for them using two functions of PHP called explode() and pathinfo().
Getting extension of the file using explode() function
function getFileExtension($fileName)
{
$parts=explode(".",$fileName);
return $parts[count($parts)-1];
}
As you can see in the first line of the function, the string is separated with dot(.) character and the last array is returned as the extension of the filename.
Now let’s look at the better approach of finding extension even filename from the path using a useful function called pathinfo()
Getting file name using basename() function
echo basename('/var/website/htdocs/test.php'); //output test.php
echo basename('/var/website/htdocs/test.php','.php'); //output test
The first line outputs test.php in the browser. The basename() function returns the file name component of the path. And in the second the statement the suffix is supplied, if suffix is supplied as the second argument then it will cut off suffix from the file name.
Getting extension of the file using pathinfo() function
Pathinfo() is a very useful function for getting extension, filenames well as the directory name.This functions returns the path information in a array with the following elements dirname, basename and extension. Let’s look at the example of pathinfo() function first
$parts = pathinfo('/var/website/htdocs/index.php');
echo $parts ['basename'], "<br />";
echo $parts ['extension'], "<br />";
echo $parts ['dirname'], "<br />";
Let output of the above will be
index.php - this is file name
php - this is extension
/var/website/htdocs/ - and this is directory name
Now i don’t have to explain about the following function which returns file extension with getFileExtension() from a path or filename and name of the file with getFileName() from a path.
//function to return file extension from a path or file name
function getFileExtension($path)
{
$parts=pathinfo($path);
return $parts['extension'];
}
//function to return file name from a path
function getFileName($path)
{
$parts=pathinfo($path);
return $parts['basename'];
}
Popularity: 13% [?]
Follow me on twitter at http://twitter.com/roshanbh.
Related Posts
» Finding difference of days between two dates in PHP
» Ip address validation in PHP using regular expression
» Useful flash Components for your website
» Hide .php extension with url rewriting using .htaccess
Comments
5 Responses to “Getting filename and extension in PHP using explode() ,basename() and pathinfo()”
Leave a Reply






[...] Getting filename and extension in PHP using explode() and pathinfo() By Roshan Getting the extension from a file name or name of the file from the full path is not a big hassle for the experienced programmer but it might be a annoying task for the beginners of PHP. Now let me find the solution for them using two … Roshan Bhattarai’s Blog - PHP… - http://roshanbh.com.np [...]
your method is neat, but i don’t understand why you didn’t use php’s built-in function … basename();
it will automatically get the filename found at the end of a path, and all you would have to do is retrieve a substring from the . to the length of the filename….
ya will you are absolutely right.I agree with you.
sorry that i’ve forgotten to mention about that function, I will sure update the post thanks a lot for reminding..
hi,
this trcks are working but i have one problem
that, i am stored uploaded path in db like “/photo/Thumb/f/image.jpg”. When will search images form name like “image” then it will give proper output but i will put “.jpg” or “*.jpg” it can’t work. So what should i do? please help and guide me
to overcome from that problem.
Thanks.
Regards
Chetan Chopkar
Are you searching using database query then you can use “%” wild card character
where images like “image%”
hope this helps..