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

技术笔记

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

豫唐网络2023-03-06 09:061139

获取当前目录下指定类型文件列表,比如或者txt文件,jpg文件等等。

应用案例

$url="D:/wwwroot/ytecn.com/txt/";
$list=GetFilesInDir($url,'txt');
print_r($list);

输出结果为:

Array ( [1] => D:/wwwroot/ytecn.com/txt/1.txt [2] => D:/wwwroot/ytecn.com/txt/2.txt )

函数

//@paramstring$dir目录
//@paramstring$type文件类型,以|分隔
//@returnarray文件列表
functionGetFilesInDir($dir,$type)
{
$files=array();
$dir=str_replace('\\','/',$dir);
if(substr($dir,-1)!=='/'){
$dir.='/';
}
if(!is_dir($dir)){
returnarray();
}

if(function_exists('scandir')){
foreach(scandir($dir)as$f){
if($f!="."&&$f!=".."&&is_file($dir.$f)){
foreach(explode("|",$type)as$t){
$t='.'.$t;
$i=strlen($t);
if(substr($f,-$i,$i)==$t){
$sortname=substr($f,0,(strlen($f)-$i));
$files[$sortname]=$dir.$f;
break;
}
}
}
}
}else{
$handle=opendir($dir);
if($handle){
while(false!==($file=readdir($handle))){
if($file!="."&&$file!=".."){
if(is_file($dir.$file)){
foreach(explode("|",$type)as$t){
$t='.'.$t;
$i=strlen($t);
if(substr($file,-$i,$i)==$t){
$sortname=substr($file,0,(strlen($file)-$i));
$files[$sortname]=$dir.$file;
break;
}
}
}
}
}
closedir($handle);
}
}

return$files;
}


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

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

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

分享给朋友:

相关文章

php将编码转换为UTF8

php将编码转换为UTF8

主要用于编码不统一导致出现乱码的情况,此函数会自动监测非UTF8编码转成UTF8编码。functionConverCode($str){ $encode=mb_detect_encoding($st...

php远程提交post函数

php远程提交post函数

远程提交方式:post范围:所有php类型程序函数代码functionpost($params,$url){ $ch=curl_init(); curl_setopt($ch,CURLOPT_UR...

php删除连续空格

php删除连续空格

对字段中的连续空格进行删除只保留一个。应用案例$txt="abcytecn1"; $name=RemoveMoreSpaces($sss); echo$name;输出结果为:a...

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

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

获取目录下所有子目录的文件夹列表(递归函数返回的是路径的全称)应用案例$url="D:/wwwroot/ytecn.com/ui/"; $list=RemoveMoreSpace...

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

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

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