Home > Backend Development > PHP Tutorial > PHP function to read folders and files in folders

PHP function to read folders and files in folders

WBOY
Release: 2016-07-25 08:55:57
Original
1313 people have browsed it
  1. /**

  2. * Functions to read folders and files in them
  3. * edit: bbs.it-home.org
  4. *
  5. */
  6. function GetFoldersAndFiles( $resourceType, $currentFolder )
  7. {
  8. // Map the virtual path to the local server path.
  9. $sServerDir = ServerMapFolder( $resourceType, $currentFolder, 'GetFoldersAndFiles' ) ;

  10. // Arrays that will hold the folders and files names.

  11. $aFolders = array() ;
  12. $aFiles = array() ;

  13. $oCurrentFolder = opendir( $sServerDir ) ;

  14. while ( $sFile = readdir( $oCurrentFolder ) )

  15. {
  16. if ( $sFile != '.' && $sFile != '..' )
  17. {
  18. if ( is_dir( $sServerDir . $sFile ) )
  19. $aFolders[] = '' ;
  20. else
  21. {
  22. $iFileSize = @filesize( $sServerDir . $sFile ) ;
  23. if ( !$iFileSize ) {
  24. $iFileSize = 0 ;
  25. }
  26. if ( $iFileSize > 0 )
  27. {
  28. $iFileSize = round( $iFileSize / 1024 ) ;
  29. if ( $iFileSize < 1 ) $iFileSize = 1 ;
  30. }

  31. $aFiles[] = '' ;

  32. }
  33. }
  34. }

  35. // Send the folders

  36. natcasesort( $aFolders ) ;
  37. echo '' ;

  38. foreach ( $aFolders as $sFolder )

  39. echo $sFolder ;

  40. echo '' ;

  41. // Send the files

  42. natcasesort( $aFiles ) ;
  43. echo '' ;

  44. foreach ( $aFiles as $sFiles )

  45. echo $sFiles ;

  46. echo '' ;

  47. }

复制代码

您可能感兴趣的文章: php遍历文件夹下并实现编辑与删除的代码 php获取文件夹大小的函数 Php删除指定文件与文件夹的方法 PHP遍历文件和文件夹的小例子 php获取文件夹信息的统计函数 php计算指定文件夹信息(文件夹数,文件数,文件夹大小)的代码 php遍历目录下所有文件和子文件夹的代码 php遍历文件夹及其下所有文件的代码 计算文件夹大小的php代码



source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template