• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php 目录遍历、删除 函数的使用介绍_php实例

php 搞代码 3年前 (2022-01-25) 12次浏览 已收录 0个评论

小编今天没事写了目录想关的函数

包括 遍历该文件夹下的文件,目录子目录 读取当前文件下目录和文件 删除当前文件夹下的目录子目录以及文件 以上三个函数目前还不支持中文文件 中文目录

<?php<BR>header("Content-type:text/html;charset=utf-8");<BR>/**<BR>* 读取当前目录下的文件和目录<BR>* <BR>* @param    string    $path    路径<BR>* @return    array    所有满足条件的文件<BR>*/<BR>function tlist($path){<BR>    $path = iconv('utf-8', 'gbk', $path);<BR>    if(!is_dir($path)){<BR>        throw new Exception($path."不是目录");<BR>    }<BR>    $arr = array('dir'=>array(),'file'=>array());<BR>    $hd = opendir($path);<BR>    while(($file = readdir($hd))!==false){<BR>        if($file=="."||$file=="..") {continue;}<BR>        if(is_dir($path."/".$file)){<BR>            $arr['dir'][] = iconv('gbk','utf-8',$file);<BR>        }else if(is_file($path."/".$file)){<BR>            $arr['file'][] = iconv('gbk','utf-8',$file);<BR>        }<BR>    }<BR>    closedir($hd);<BR>    echo "目录有:".implode("<br />",$arr['dir'])."<br />";<BR>    echo "文件有:".implode("<br />",$arr['file']);<BR>}<BR>/**<BR>* 遍历当前目录下的文件和目录以及子文件夹中目录<BR>* <BR>* @param    string    $path    路径<BR>* @return    array    所有满足条件的文件<BR>*/<BR>function blist($path){<BR>    if(!is_dir(iconv("utf-8","gbk",$path))){<BR>    throw new Exception("文件夹".$path."不存在或者不是文件");<BR>   }<BR>    $arr = array();<BR>    $hd = opendir(iconv("utf-8","gbk",$path));<BR>    while(($file = readdir($hd))!==false){<BR>        if($file=="."||$file=="..") {continue;}<BR>          $newpath=iconv('utf-8', 'gbk', $path) .'/'.$file;<BR>        if(is_dir($newpath)){<BR>            $arr[] = blist($path."/".$file);<BR>        }else if(is_file($newpath)){<BR>            $arr[] = iconv('gbk','utf-8',$file);<BR>        }<BR>   <strong>+本文来源gao@daima#com搞(%代@#码网</strong><pre>搞代gaodaima码

}
closedir($hd);
return $arr;
}
/**
* 删除目录下的文件以及子目录
* #param string $path 路径
* #return string 删除成功返回true 失败返回false;
*/
function dirDel($path){
if(!is_dir($path)){
throw new Exception($path.”输入的不是有效目录”);
}
$hand = opendir($path);
while(($file = readdir($hand))!==false){
if($file==”.”||$file==”..”) continue;
if(is_dir($path.”/”.$file)){
dirDel($path.”/”.$file);
}else{
@unlink($path.”/”.$file);
}

}
closedir($hand);
@rmdir($path);
}
?>


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php 目录遍历、删除 函数的使用介绍_php实例
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址