Subscribe to Tutorial Feed

Flash and PHP Bible

The Flash and PHP Bible has been released! The book can be found on Amazon or wherever fine books are sold in your area.

The Flash and PHP Bible has a forum for quick support.

Scriptplayground » tutorials » php » Get File Extension

Get File Extension

Get a file extension, very useful for a file uploader.

function getExtension($file) {
  $pos = strrpos($file, '.');
  if(!$pos) {
    return 'Unknown Filetype';
  $str = substr($file, $pos, strlen($file));
  return $str;

First check for a dot '.' which would mean a valid file extension, if one is not found return "Unknown Filetype" and exit the function. Assuming we now found a dot in the file name we can move on to grabbing the actual file extension.

We achieve this using substr(). The first argument is the file name, the second is the position of the last dot, and the third argument is file name length.

The last step is to return that file extension to be used in whatever part of our script we need.

Here is a quick code snippet/example.

$ext = getExtension('mycoolfile.swf');
switch($ext) {
  case 'php' :
    print "File is a PHP file";
  case 'jpg' :
    print "File is a JPEG";
  case 'swf' :
    print "File is an SWF or Flash format.";

Follow Scriptplayground on Twitter (@scriptplay)

Comments: Get File Extension

 Robin   Tue Apr 3, 2007 9:59 am  
i need a php file extension
 Jhecht  Fri Jun 8, 2007 11:03 pm  
Usually i use RegEXP to check for 3-4 characters after a dot near the back of the string.. it works fine. Never thought about doing it with strrpos() though.
 rachael jean harper  Mon Jul 16, 2007 3:48 pm  
I am trying to recieve better video reception on my pc. Thanks
 Matthew  Mon Jun 2, 2008 7:48 pm  
i made this:
$Ext = substr($FilePath, strrpos($imagepath,".") + 1);
 huy  Thu Jul 17, 2008 11:32 am  
What about file names that are like this "image.1.jpg", what would happen in this case?
 sqsqw  Tue Aug 19, 2008 2:07 am  
 Steve  Fri Aug 22, 2008 8:43 pm  
I use a different way. This gets the extension no matter how many dots are in the file name, like name.1.2.doc for example. Also, the extension can have more or less than 3 characters.

$parts = explode('.',$filename);
$ext = $parts[count($parts)-1];
 Suleman  Mon Sep 1, 2008 6:24 am  
You could just the the pathinfo PHP function (

$path_parts = pathinfo('/www/htdocs/index.html');

echo $path_parts['dirname'], "n";
echo $path_parts['basename'], "n";
echo $path_parts['extension'], "n";
echo $path_parts['filename'], "n"; // since PHP 5.2.0
 Jason  Thu Oct 29, 2009 9:08 pm  
$filename = "index.old.html";
$ext = '';
if (preg_match('/.([^.]*)$/', $filename, $m)) {
$ext = $m[1];
 Jason  Thu Oct 29, 2009 9:09 pm  
In my previous code, the first period after preg_match needs a backslash immediately before it. For some reason it got stripped out by the comment system.
 pimpgear  Mon Dec 14, 2009 3:07 am  
array_pop(explode('.', $filename));
 Joshua D. Burns  Fri Sep 10, 2010 11:58 am  
Your "getExtension()" function is actually one of the slowest ways to get a file's extension.

PHP has a built-in pathinfo() function which is compiled C code and is on average 1/10 faster than the leading alternative:
substr(strrchr($filename, '.'), 1);

Your best bet is:
pathinfo($filename, PATHINFO_EXTENSION)
Add a comment
The ability to add new comments has been disabled in this archived version.

Main | Tutorials | Articles