技术笔记
当前位置:首页 > 商学院 > 技术笔记 > 正文内容

技术笔记

php获取目录下文件夹列表(递归)

豫唐网络2023-03-06 08:161062

获取目录下所有子目录的文件夹列表(递归函数返回的是路径的全称)

应用案例

$url="D:/wwwroot/ytecn.com/ui/";
$list=RemoveMoreSpaces($url);
print_r($list);

输出结果为:

Array ( [0] => D:/wwwroot/ytecn.com/ui/code/list [1] => D:/wwwroot/ytecn.com/ui/code [2] => D:/wwwroot/ytecn.com/ui/css [3] => D:/wwwroot/ytecn.com/ui/img [4] => D:/wwwroot/ytecn.com/ui/js [5] => D:/wwwroot/ytecn.com/ui/tinymce/langs [6] => D:/wwwroot/ytecn.com/ui/tinymce/skins [7] => D:/wwwroot/ytecn.com/ui/tinymce )

函数

functionGetDirsInDir_Recursive($dir)
{
$dirs=array();

if(!file_exists($dir)){
returnarray();
}
if(!is_dir($dir)){
returnarray();
}
$dir=str_replace('\\','/',$dir);
if(substr($dir,-1)!=='/'){
$dir.='/';
}

if(function_exists('scandir')){
foreach(scandir($dir,0)as$d){
if(is_dir($dir.$d)){
if(($d!='.')&&($d!='..')){
$array=GetDirsInDir($dir.$d);
if(count($array)>0){
foreach($arrayas$key=>$value){
$dirs[]=$dir.$d.'/'.$value;
}
}
$dirs[]=$dir.$d;
}
}
}
}else{
$handle=opendir($dir);
if($handle){
while(false!==($file=readdir($handle))){
if($file!="."&&$file!=".."){
if(is_dir($dir.$file)){
$array=GetDirsInDir($dir.$file);
if(count($array)>0){
foreach($arrayas$key=>$value){
$dirs[]=$dir.$file.'/'.$value;
}
}
$dirs[]=$dir.$file;
}
}
}
closedir($handle);
}
}

return$dirs;
}


扫描二维码推送至手机访问。

版权声明:本文由汤阴县豫唐网络科技有限公司发布,如需转载请注明出处。

本文链接:https://www.ytecn.com/post/561.html

分享给朋友:

相关文章

vscode运行php和Composer

vscode运行php和Composer

需要用到的工具1、安装php(官网下载)2、安装composer(官网)3、vscode插件PHP Server4、vscode插件PHP Debug5、windows 11系统步骤1、安装php安装...

php分割string并取某项数据

php分割string并取某项数据

对string进行分割,并取某项数据。应用案例$txt="姓名|电话|手机号|豫唐"; $name=SplitAndGet($txt,"|",3); ech...

php获取当前目录下文件夹列表

php获取当前目录下文件夹列表

获取当前目录下文件夹的名称应用案例$url="D:/wwwroot/ytecn.com/ui/"; $list=GetDirsInDir($url); print_r($lis...

获取当前目录下指定类型文件列表

获取当前目录下指定类型文件列表

获取当前目录下指定类型文件列表,比如或者txt文件,jpg文件等等。应用案例$url="D:/wwwroot/ytecn.com/txt/"; $list=GetFilesInD...

PHP获取文件后缀名

PHP获取文件后缀名

获取文件后缀名,识别当前文件是什么类型的。应用案例$url="1.txt"; $name=GetFileExt($url); print_r($name);输出结果为:txt函...

php在字符串型的参数表中新加参数删除参数查询参数

php在字符串型的参数表中新加参数删除参数查询参数

字符串型的参数表加入一个新参数,从字符串型的参数表中删除一个参数,在字符串参数值查找参数。加入新参数应用案例$array="1|2|3|4"; $name=AddNameInSt...