This article brings you code examples about PHP traversing all files in a folder. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Whether it is an interview or normal work, you will need to traverse all the files in the folder. Take notes today. Without further ado, let’s get straight to the code:
'; 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);
Running results:
The source code has been uploaded to GitHub: https://github .com/cuiyuanxin/php-demo/blob/master/dir.php
The above is the detailed content of Code example for php to traverse all files in a folder. For more information, please follow other related articles on the PHP Chinese website!