Home > Backend Development > PHP Tutorial > PHP code to traverse all files and subfolders in a directory

PHP code to traverse all files and subfolders in a directory

WBOY
Release: 2016-07-25 08:58:10
Original
883 people have browsed it
Introducing a code that can traverse all files and subfolders in a directory for reference by beginners.

Example:

<?php
/**
* 遍历目录下所有文件及子文件夹
* edit bbs.it-home.org
*/
function read_all_dir ( $dir )
{
$result = array();
$handle = opendir($dir);
if ( $handle )
{
while ( ( $file = readdir ( $handle ) ) !== false )
{
if ( $file != '.' && $file != '..')
{
$cur_path = $dir . DIRECTORY_SEPARATOR . $file;
if ( is_dir ( $cur_path ) )
{
$result['dir'][$cur_path] = read_all_dir ( $cur_path );
}
else
{
$result['file'][] = $cur_path;
}
}
}
closedir($handle);
}
return $result;
}
?>
Copy after login

The code is not complicated. If you are interested, you can find a few directories and test it yourself.



source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template