PHPEdit.net Community
   
1 2 3 4
Tutorials Tips Pasties Code Snippets
 

Home > Code snippets > PHP > Files > Validate file extension

Validate file extension

Created by Sebastien Hordeaux, last update on 26/02/2008 17:31

This simple function help you to validate that a file has an allowed extension.

  1. function validate_extension($filename, $validExtensions = array('zip',
    'rar', 'jpg', 'jpeg', 'gif', 'bmp'))
  2. {
  3. // find the last dot inside the filename
  4. $dotPos = strrpos($filename, '.');
  5. // if no dot was found, the extension is not valid
  6. if (false === $dotPos) {
  7. return false;
  8. }
  9. // get the extension, be sure the dot is not included, and be sure
    the
  10. // string gets to lowercase
  11. $extension = substr($filename, ($dotPos + 1));
  12.  
  13. $extension = strtolower($extension);
  14. // when the extension is inside the array, it is valid
  15. return in_array($extension, $validExtensions);
  16. }

Dependencies

No special requirements are needed by this snippet

By logging in you will be able to:

  • Recommand this page to someone else
  • Monitor changes on this item
  • Rate this item
  • Post comments
  • Download this item
Login now!

 
PHPEdit User Community, © 2008 WaterProof SARL
Powered by PHPEdit