Example of how to traverse all files in a folder using PHP

little bottle
Release: 2023-04-06 06:20:01
forward
3962 people have browsed it

Whether it is for interviews or normal work, it is necessary to traverse all files in a folder. Let me share with you an example of how to traverse all files in a folder using PHP for your reference.

'; dirList($dir_path . '/' . $file); } else { echo $dir_path . '/' . $file . '
'; } } } closedir($dirs); } } else { echo '目录不存在!'; } } dirList('/var/www/html/php-demo'); function dir_list($dir) { if(!is_dir($dir)) return false; $dir_list = array(); $opendir = opendir($dir); if($opendir) { while(($file = readdir($opendir)) !== false) { if($file !== '.' && $file !== '..') { $tem = $dir . '/' . $file; if(is_dir($tem)) { $dir_list[$tem . '/'] = $file . '/'; dir_list($tem); } else { $dir_list[] = $file; } } } closedir($opendir); return $dir_list; } } $dir = dir_list('/var/www/html/php-demo'); var_dump($dir);
Copy after login

Running results:

Example of how to traverse all files in a folder using PHP

##Related tutorials:

PHP video tutorial

The above is the detailed content of Example of how to traverse all files in a folder using PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:cnblogs.com
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
Latest Articles by Author
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!