写一个遍历指定目录下所有子目录和子文件的函数
// 题1
class File
{
public $save_files = [];
public $temp_path = [];
public function get_files($paths)
{
$this->temp_path = [];
foreach ($paths as $path) {
foreach ($this->get_file($path) as $item) {
$item = $path . '/' . $item;
if (is_dir($item)) {
array_push($this->temp_path, $item);
} else {
array_push($this->save_files, $item);
}
}
}
if (!empty($this->temp_path)) {
$this->get_files($this->temp_path);
}
return $this->save_files;
}
public function get_file($path)
{
$data = array_flip(scandir($path));
unset($data['.']);
unset($data['..']);
return array_flip($data);
}
}
//$app = new File();
//$data = $app->get_files([ './phplog' ]); function LoopFolder($dirPath)
{
$files = [];
if ($handle = opendir($dirPath)) {
while (($file = readdir($handle)) !== false) {
if ($file != '..' && $file != '.') {
if (is_dir($dirPath.DIRECTORY_SEPARATOR.$file)) {
$files[$file] = LoopFolder($dirPath.DIRECTORY_SEPARATOR.$file);
} else {
$files[] = $file;
}
}
}
}
closedir($handle);
return $files;
}