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

Home > Pasties > Pastie #2

Created on 23/01/2008 14:59 by Tobias Tom

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

 
PHPEdit User Community, © 2008 WaterProof SARL
Powered by PHPEdit