您的位置:首页 > 编程语言 > PHP开发

PHP遍历文件夹内容

2015-08-17 10:19 579 查看
遍历文件,没什么技术含量,看了手册,还有事例代码,稍微修改一下,把文件夹下面的文件夹继续变量就OK了。
function listDir($dir){
//判断是否是文件夹
if(is_dir($dir)){
// 打开文件夹
if ($dh = opendir($dir)) {
//读取文件夹
while (($file = readdir($dh)) !== false) {
//如果文件夹中还有文件夹就继续遍历
//把 .和..排除
if(is_dir($dir.'/'.$file) && $file != '.' && $file != '..'){

echo '<hr>'."文件夹: $file ". "<br>";
//继续遍历
listDir($dir."/".$file."/");
}else{
//输出文件夹里面的内容
if($file != '.' && $file != '..')
echo $file.'<br>';
}

}
closedir($dh);
}
}
}

listDir("D:\soft");




删除一个文件夹目录

function deldir($dir) {
$dh=opendir($dir);
while ($file=readdir($dh)) {
if($file!="." && $file!="..") {
$fullpath=$dir."/".$file;
if(!is_dir($fullpath)) {
unlink($fullpath);
} else {
deldir($fullpath);
}
}
}
closedir($dh);
if(rmdir($dir)) {
return true;
} else {
return false;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: