files by extension


/ Published in: PHP
Save to your folder(s)

dir_list('data/', 'txt'); => returns all *.txt files from the data/ folder


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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.