Files by extension


SUBMITTED BY: phpsnippets

DATE: Oct. 21, 2015, 2:15 p.m.

FORMAT: Text only

SIZE: 619 Bytes

HITS: 1857

  1. function get_files_by_ext($path, $ext){
  2. $files = array();
  3. if (is_dir($path)){
  4. $handle = opendir($path);
  5. while ($file = readdir($handle)) {
  6. if ($file[0] == '.'){ continue; }
  7. if (is_file($path.$file) and preg_match('/\.'.$ext.'$/', $file)){
  8. $files[] = $file;
  9. }
  10. }
  11. closedir($handle);
  12. sort($files);
  13. }
  14. return $files;
  15. }
  16. /*
  17. ** Example:
  18. */
  19. print_r(get_files_by_ext('data/', 'txt'));
  20. /*
  21. returns:
  22. Array
  23. (
  24. [0] => readme_1.txt
  25. [1] => readme_2.txt
  26. )
  27. */

comments powered by Disqus