search
HomeBackend DevelopmentPHP TutorialPHP iterate through directory and file lists

  1. define('DS', DIRECTORY_SEPARATOR);
  2. class getDirFile{
  3. //返回数组
  4. private $DirArray = array();
  5. private $FileArray = array();
  6. private $DirFileArray = array();
  7. private $Handle,$Dir,$File;
  8. //获取目录列表
  9. public function getDir( & $Dir ){
  10. if( is_dir($Dir) ){
  11. if( false != ($Handle = opendir($Dir)) ){
  12. while( false != ($File = readdir($Handle)) ){
  13. if( $File!='.' && $File!='..' && !strpos($File,'.') ){
  14. $DirArray[] = $File;
  15. }
  16. }
  17. closedir( $Handle );
  18. }
  19. }else{
  20. $DirArray[] = '[Path]:''.$Dir.'' is not a dir or not found!';
  21. }
  22. return $DirArray;
  23. }
  24. //获取文件列表
  25. public function getFile( & $Dir ){
  26. if( is_dir($Dir) ){
  27. if( false != ($Handle = opendir($Dir)) ) {
  28. while( false != ($File = readdir($Handle)) ){
  29. if( $File!='.' && $File!='..' && strpos($File,'.') ){
  30. $FileArray[] = $File;
  31. }
  32. }
  33. closedir( $Handle );
  34. }
  35. }else{
  36. $FileArray[] = '[Path]:''.$Dir.'' is not a dir or not found!';
  37. }
  38. return $FileArray;
  39. }
  40. //获取目录/文件列表
  41. public function getDirFile( & $Dir ){
  42. if( is_dir($Dir) ){
  43. $DirFileArray['DirList'] = $this->getDir( $Dir );
  44. if( $DirFileArray ){
  45. foreach( $DirFileArray['DirList'] as $Handle ){
  46. $File = $Dir.DS.$Handle;
  47. $DirFileArray['FileList'][$Handle] = $this->getFile( $File );
  48. }
  49. }
  50. }else{
  51. $DirFileArray[] = '[Path]:''.$Dir.'' is not a dir or not found!';
  52. }
  53. return $DirFileArray;
  54. }
  55. }
  56. ?>
复制代码

实例:(相对路径或绝对路径)

1.获取目录列表
  1. $Dir_dir = './example';
  2. $getDirFile = new getDirFile();
  3. $getDir = $getDirFile->getDir( $Dir_dir );
  4. print_r($getDir);
  5. ?>
  6. 显示:
  7. [html] view plaincopy
  8. Array
  9. (
  10. [0] => example_one
  11. [1] => example_two
  12. )
复制代码

2.获取文件列表
  1. $File_one_dir = './example/example_one';
  2. $File_two_dir = 'E:/Workspace/mycode/getDirFile/example/example_two';
  3. $getDirFile = new getDirFile();
  4. $getFile_one = $getDirFile->getFile( $File_one_dir );
  5. $getFile_two = $getDirFile->getFile( $File_two_dir );
  6. print_r($getFile_one);
  7. print_r($getFile_two);
  8. ?>
复制代码

显示:
  1. Array
  2. (
  3. [0] => example.sql
  4. [1] => example.txt
  5. )
  6. Array
  7. (
  8. [0] => example.php
  9. )
复制代码

3.获取目录/文件列表
  1. $Dir_dir = './example';
  2. $getDirFile = new getDirFile();
  3. $getDirFile = $getDirFile->getDirFile( $Dir_dir );
  4. print_r($getDirFile);
  5. ?>
复制代码

显示:
  1. Array
  2. (
  3. [DirList] => Array
  4. (
  5. [0] => example_one
  6. [1] => example_two
  7. )
  8. [FileList] => Array
  9. (
  10. [example_one] => Array
  11. (
  12. [0] => example.sql
  13. [1] => example.txt
  14. )
  15. [example_two] => Array
  16. (
  17. [0] => example.php
  18. )
  19. )
  20. )
复制代码

File list, PHP


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
PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.