php traverses all the directories and files in a folder

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags echo file files function is_dir network network programming php

We often encounter this problem in the interview: PHP Traverse all the files and subfolders under a folder.

There are many solutions to this problem. But the general idea is the same. Recursive.

$ handle = opendir ($ path); while (false! == ($ handle = $ ./filepath); function getfiles ($ path) {if (! is_dir ($ path)) return; if (is_dir ($ path2)) {echo ($ path! = '.' & amp; $ file! = '..') {$ path2 = $ path. '/' echo $ file; getfiles ($ path2);} else {echo '' echo $ file;}}} print_r (getfiles ($ path)); echo '<HR>'; function getdir ($ path) {if (! is_dir ($ path)) return; $ handle = dir ($ path); while ($ file = $ handle- & gt; read ()) {if ($ file! = '.' & amp; if (is_dir ($ path2)) {echo $ file. "t"; getdir ($ path2);} else {echo $ file. ' ($ path) as $ single) {if ($ single! ';}}}} getdir ($ path); echo' <HR> 'function get_dir_scandir ($ path) ($ path2 = $ path. '/'. $ single; if (is_dir ($ path2)) {echo $ single. "rn"; get_dir_scandir ( $ path = array ();} else {echo $ single. "rn";}}} get_dir_scandir ($ path); echo '<HR>'; function get_dir_glob / curl / * ') as $ single) {echo $ single. "rn";}} get_dir_glob (); echo' <HR> '; function myscandir ($ path) {if (! is_dir ($ path)) return; foreach (scandir ($ path) as $ file) {if ($ file! = '.' & amp; $ file! = '..') {$ path2 = $ path. '/'. $ file; if ( is_dir ($ path2)) {echo }}}} myscandir ($ path); echo '<HR>'; function myglob ($ path) {$ path_pattern = $ path. ' / * '; foreach (glob ($ path_pattern) as $ file) {if (is_dir ($ file)) {echo $ file; myscandir ($ file);} else {echo $ file.' ';}}} myglob ( $ path);
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.