Home  >  Article  >  Backend Development  >  php code lists all files in a directory

php code lists all files in a directory

WBOY
WBOYOriginal
2016-07-25 09:08:051463browse
  1. /**
  2. Directory processing function
  3. */
  4. function dir_path($path) {
  5. $path = str_replace('\', '/', $path);
  6. if (substr($ path, -1) != '/') $path = $path . '/';
  7. return $path;
  8. }
  9. /**
  10. * List all files in the directory
  11. *
  12. * @param str $path directory
  13. * @param str $exts suffix
  14. * @param array $list path array
  15. * @return array Return path array
  16. */
  17. function dir_list($path, $exts = '', $list = array()) {
  18. $path = dir_path($path);
  19. $files = glob($path . '*');
  20. foreach($files as $v) {
  21. if (!$exts || preg_match("/.($exts)/i", $v)) {
  22. $list[] = $v;
  23. if (is_dir($v)) {
  24. $list = dir_list($v, $exts, $ list);
  25. }
  26. }
  27. }
  28. return $list;
  29. }
  30. ?>
Copy code

Usage:

  1. $r = dir_list('dir');
  2. printf("

    The output data is:

    %s
    n" , var_export($r, true));
  3. ?>
Copy code

#------------------- PHP function-used to list all files in a directory

A function written in PHP to list all files in a specified directory. The function is followed by a sample code for its use. Note: If the page is UTF-8, in the Chinese version of Windows system, garbled characters will appear when reading the Chinese file name.

  1. /* Function listDirTree( $dirName = null )
  2. ** Function lists all files and subdirectories in the directory
  3. ** Parameter $dirName directory name
  4. ** Returns the directory structure array false Failed
  5. */
  6. function listDirTree( $dirName = null )
  7. {
  8. if( empty( $dirName ) )
  9. exit( "IBFileSystem: directory is empty." );
  10. if( is_dir( $dirName ) )
  11. {
  12. if( $dh = opendir( $dirName ) )
  13. {
  14. $tree = array();
  15. while( ( $file = readdir( $dh ) ) !== false )
  16. {
  17. if( $file != ". " && $file != ".." )
  18. {
  19. $filePath = $dirName . "/" . $file;
  20. if( is_dir( $filePath ) ) //For directories, recursively
  21. {
  22. $tree[$file ] = listDirTree( $filePath );
  23. }
  24. else //For files, add to the current array
  25. {
  26. $tree[] = $file;
  27. }
  28. }
  29. }
  30. closedir( $dh );
  31. }
  32. else
  33. {
  34. exit( "IBFileSystem: can not open directory $dirName.");
  35. }
  36. //Return the current $tree
  37. return $tree;
  38. }
  39. else
  40. {
  41. exit( "IBFileSystem: $dirName is not a directory .");
  42. }
  43. }
  44. $files = listDirTree(".");
  45. //print_r($files);
  46. $size = count(files);
  47. //The following code creates a file in this directory List (with link address)
  48. echo '
      ';
    1. for( $i=0; $files[$i] != NULL; $i++ ) {
    2. echo '
    3. '.$files[$i].'
    4. ';
    5. }
    6. echo '< /ol>';
    7. ?>
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