Home  >  Article  >  Backend Development  >  PHP code example for reading directory list and file list

PHP code example for reading directory list and file list

WBOY
WBOYOriginal
2016-07-25 08:59:291129browse
  1. /**

  2. * getDir() goes to the folder list
  3. * getFile() goes to the file list under the corresponding folder
  4. * The difference between the two is to determine whether there are files with a "." suffix, everything else is the same
  5. * edit bbs.it-home .org
  6. */

  7. //Get the file directory list, this method returns an array

  8. function getDir($dir ) {
  9. $dirArray[]=NULL;
  10. if (false != ($handle = opendir ( $dir ))) {
  11. $i=0;
  12. while ( false !== ($file = readdir ( $handle ) ) ) {
  13. //Remove "".", ".." and files with ".xxx" suffix
  14. if ($file != "." && $file != ".."&&!strpos($file ,".")) {
  15. $dirArray[$i]=$file;
  16. $i++;
  17. }
  18. }
  19. //Close the handle
  20. closedir ( $handle );
  21. }
  22. return $dirArray;
  23. }
  24. //Get the file list

  25. function getFile($dir) {
  26. $fileArray[]=NULL;
  27. if (false != ($handle = opendir ( $dir ))) {
  28. $i= 0;
  29. while ( false !== ($file = readdir ( $handle )) ) {
  30. //Remove "".", ".." and files with ".xxx" suffix
  31. if ($file != "." && $file != ".."&&strpos($file,".")) {
  32. $fileArray[$i]="./imageroot/current/".$file;
  33. if($i== 100){
  34. break;
  35. }
  36. $i++;
  37. }
  38. }
  39. //Close the handle
  40. closedir ( $handle );
  41. }
  42. return $fileArray;
  43. }

  44. //Call Method getDir("./dir")...can be an absolute path or a relative path

  45. ?>

Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn