Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does PHP get all the contents in the directory RecursiveDirectoryIterator

2025-04-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "PHP how to get all the contents of the directory RecursiveDirectoryIterator", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "PHP how to get all the contents of the directory RecursiveDirectoryIterator" bar!

PHP gets all the contents in the directory RecursiveDirectoryIterator

This time we introduce a directory iterator in the SPL library. Its function is actually very simple, as can be seen from the name, which is to get all the contents of the specified directory. Before we have to traverse the directory to get the directory and all the files under the directory generally need to recursively traverse, to write this code to tell the truth is quite troublesome, so PHP prepared this set of built-in API for us, when entering the specified directory, directly return to all the subdirectories and file contents under that directory. Of course, it is not a tree, the order is not necessarily, we have to deal with it if we want to organize it into a tree.

Don't say much, just look at the code:

$path = $argv [1]

/ / get all the contents in the directory

$dirs = new RecursiveIteratorIterator (new RecursiveDirectoryIterator ($path), RecursiveIteratorIterator::SELF_FIRST)

Foreach ($dirs as $k = > $d) {

Echo 'key:'. $k, PHP_EOL

If ($d-> isDir ()) {

Echo $d-> getPathname (), PHP_EOL

} else {

Echo $d-> getFilename (), PHP_EOL

}

}

/ / execute php PHP to get all the contents of the directory RecursiveDirectoryIterator.php.. /

/ / key:../.

/ /.. /.

/ / key:../..

/ /.. /..

/ / key:../source

/ /.. / source

/ / key:../source/.

/ /.. / source/.

/ / key:../source/..

/ /.. / source/..

/ / key:../source/PHP gets all the contents of the directory RecursiveDirectoryIterator.php

/ / PHP gets all the contents of the directory RecursiveDirectoryIterator.php

/ / key:../source/PHP large file read operation .php

/ / PHP large file read operation .php

/ / key:../PHP large file read operation .md

/ / PHP large file read operation .md

/ / key:../PHP gets all the contents of the directory RecursiveDirectoryIterator.md

/ / PHP gets all the contents of the directory RecursiveDirectoryIterator.md

It's actually just one line of code, and then directly loop out the iterator. From the results, we can see that we first enter the source directory and then traverse the external file contents, and get all the contents under the directory according to the order of directory and file name. Is it much more convenient than writing recursive functions ourselves?

What if we want to get all the PHP files in the directory and calculate their total file size? Using this set of iterators can also be very simple, we just need to add a regular iterator to filter the previous iterator content:

/ / get all php files

$regIts = new RegexIterator ($dirs,'/ ^. +\ .php $/ i')

$fileSize = 0

Foreach ($regIts as $k = > $p) {

Echo $p-> getSize (). ''. $k, PHP_EOL

$fileSize + = $p-> getSize ()

}

Echo 'Total', $fileSize, PHP_EOL

/ / 622.. / source/PHP gets all the contents of the directory RecursiveDirectoryIterator.php

/ / 869.. / source/PHP large file read operation .php

/ / Total 1491

It feels like ls-l, which makes it easy for us to do relevant operations under the directory. The use of this class is simply introduced here, there are many capabilities worth exploring in the SPL library, slowly learn and practice, and constantly improve our ability to face elegant programming.

Test code: https://github.com/zhangyue0503/dev-blog/blob/master/php/202003/source/PHP%E8%8E%B7%E5%8F%96%E7%9B%AE%E5%BD%95%E4%B8%AD%E7%9A%84%E5%85%A8%E9%83%A8%E5%86%85%E5%AE%B9RecursiveDirectoryIterator.php

Reference document: "PHP7 programming practice" https://www.php.net/manual/en/class.recursivedirectoryiterator.phphttps://www.php.net/manual/en/class.splfileinfo.php

Thank you for your reading, the above is the content of "PHP how to get all the contents of the directory RecursiveDirectoryIterator", after the study of this article, I believe you have a deeper understanding of how PHP gets all the contents of the directory RecursiveDirectoryIterator, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 296

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report