今天的需求要在一个文件夹中读取出这个文件夹下所有的文件,当然也包括这个文件夹下面所有的子文件夹,当然网上有很多的教程,但为了自己理解得更加深刻,还是自己写一下吧。代码如下:
$path = './use';
$result = scanFile($path);
function scanFile($path) {
global $result;
$files = scandir($path);
foreach ($files as $file) {
if ($file != '.' && $file != '..') {
if (is_dir($path . '/' . $file)) {
scanFile($path . '/' . $file);
} else {
$result[] = basename($file);
}
}
}
return $result;
}