<?php
read_all_dir_and_file('/home/jinbo/Desktop');
read_all_dir_and_file_exec('/home/jinbo/Desktop');
# php 内部方法
function read_all_dir_and_file($dir) {
if (is_dir($dir)) {
$dh = opendir($dir);
while ( ($file = readdir($dh)) !== false ) {
if (in_array($file, ['.', '..'])) {
continue;
}
$file_path = $dir.'/'.$file;
if ( is_dir($file_path) ) {
read_all_dir_and_file($file_path);
} else {
echo $file_path."\n";
}
}
closedir($dir);
} else {
echo $dir;
}
}
# php 内部方法与系统方法
function read_all_dir_and_file_exec($dir) {
exec('ls '.$dir, $dir_all, $status);
if ($status) {
foreach ($dir_all as $file) {
$file_path = $dir.'/'.$file;
if (is_dir($file_path)) {
read_all_dir_and_file_exec($file_path);
} else {
echo $file_path."\n";
}
}
}
}
PHP遍历一个目录中的所有目录与文件
最新推荐文章于 2023-02-14 11:27:01 发布