php 目录遍历、删除 函数的使用介绍
2013-04-28 00:00
731 查看
小编今天没事写了目录想关的函数
包括 遍历该文件夹下的文件,目录子目录 读取当前文件下目录和文件 删除当前文件夹下的目录子目录以及文件 以上三个函数目前还不支持中文文件 中文目录
包括 遍历该文件夹下的文件,目录子目录 读取当前文件下目录和文件 删除当前文件夹下的目录子目录以及文件 以上三个函数目前还不支持中文文件 中文目录
<?php header("Content-type:text/html;charset=utf-8"); /** * 读取当前目录下的文件和目录 * * @param string $path 路径 * @return array 所有满足条件的文件 */ function tlist($path){ $path = iconv('utf-8', 'gbk', $path); if(!is_dir($path)){ throw new Exception($path."不是目录"); } $arr = array('dir'=>array(),'file'=>array()); $hd = opendir($path); while(($file = readdir($hd))!==false){ if($file=="."||$file=="..") {continue;} if(is_dir($path."/".$file)){ $arr['dir'][] = iconv('gbk','utf-8',$file); }else if(is_file($path."/".$file)){ $arr['file'][] = iconv('gbk','utf-8',$file); } } closedir($hd); echo "目录有:".implode("<br />",$arr['dir'])."<br />"; echo "文件有:".implode("<br />",$arr['file']); } /** * 遍历当前目录下的文件和目录以及子文件夹中目录 * * @param string $path 路径 * @return array 所有满足条件的文件 */ function blist($path){ if(!is_dir(iconv("utf-8","gbk",$path))){ throw new Exception("文件夹".$path."不存在或者不是文件"); } $arr = array(); $hd = opendir(iconv("utf-8","gbk",$path)); while(($file = readdir($hd))!==false){ if($file=="."||$file=="..") {continue;} $newpath=iconv('utf-8', 'gbk', $path) .'/'.$file; if(is_dir($newpath)){ $arr[] = blist($path."/".$file); }else if(is_file($newpath)){ $arr[] = iconv('gbk','utf-8',$file); } } 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); } ?>
相关文章推荐
- php 目录遍历、删除 函数的使用介绍
- PHP使用内置dir类实现目录遍历删除
- php创建和删除目录函数介绍和递归删除目录函数分享
- php创建和删除目录函数介绍和递归删除目录函数分享
- php 目录遍历、删除 函数
- PHP目录与文件操作技巧总结(创建,删除,遍历,读写,修改等)
- JavaScript forEach()遍历函数使用及介绍
- PHP 获取文件目录权限函数fileperms介绍
- php中使用while、each与list函数组合遍历二维数组的方法
- php文件夹与文件目录操作函数介绍
- php数组函数序列 之shuffle()和array_rand() 随机函数使用介绍
- php数组函数序列 之shuffle()和array_rand() 随机函数使用介绍
- PHP删除非空目录函数
- 使用PHP遍历文件夹与子目录的函数代码
- php删除目录的函数
- PHP学习笔记——使用数组的内部指针控制函数遍历数组
- 使用PHP SPL(标准库)遍历目录
- php 遍历显示文件夹下所有目录、所有文件的函数,没有分页的代码
- php glob()函数实现目录文件遍历与寻找与模式匹配的文件路径
- PHP eval函数使用介绍