1. Home
  2. Computing & Technology
  3. PHP / MySQL

Extracting the File Extension

Finding the file extention in the file name

By Angela Bradley, About.com

function findexts ($filename)
{
$filename = strtolower($filename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}
This function is used to find the file extension. If you wanted to rename a file upload you would still need to keep the extension. We can use this function to find it. Once found it can be appended to the end of a random number or a timestamp (or other naming system you choose) to use as the file name.

Basically what the code is doing is first using strtolower to change the extension (and the whole file name) into lower case, just to keep it clean. Next we are splitting the filename into an array using split. By splitting it at the [.] the extension will be the last element in the array, which we then return.

More PHP / MySQL Quick Tips

Explore PHP / MySQL

More from About.com

  1. Home
  2. Computing & Technology
  3. PHP / MySQL
  4. Step By Steps
  5. Find File Extension PHP - Strip File Extension PHP - PHP file ext

©2008 About.com, a part of The New York Times Company.

All rights reserved.