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

Home > Code snippets > PHP > Validate file extension (smaller code)

Validate file extension (smaller code)

Created by Jose Chafardet, last update on 30/10/2008 11:38

this is a file extension validation snipped i wrote some days ago, is smaller than the existing one here, and works really fine, it uses a small regex for the comparison and it does requires the . on the ext.

  1. /**
  2. *
  3. * validate_file_ext()
  4. * Get the file extension of a file and validates if valid.
  5. * @param $filename Filename to get the extension from
  6. * @returns bool (returns true if file is allowed, false if isnt )
  7. * @author Jose L. Chafardet
  8. *
  9. */
  10. function validate_file_ext ( $filename ) {
  11. $file_ext = strrchr ( $filename, '.' );
  12. if (! preg_match (
    "/.mp3|.wma|.mov|.flv|.wmv|.mpg|.mpeg|.jpg|.jpeg|.gif|.png/",
    $file_ext )) {
  13. return false;
  14. } else {
  15. return true;
  16. }
  17. }
  18.  

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