Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  php  >  正文 php目录遍历与删除的代码一例

php目录遍历与删除的代码一例

发布时间:2015-11-09   编辑:www.jquerycn.cn
php实现的目录或文件夹的遍历,以及删除指定文件的代码,很简单,适合初学的朋友参考。

主要功能如下:
遍历该文件夹下的文件,目录子目录,读取当前文件下目录和文件,删除当前文件夹下的目录子目录以及文件。
说明:暂不支持中文目录。

<?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;
}
/**by http://www.jbxue.com
* 删除目录下的文件以及子目录
* #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删除N分钟前创建的所有文件的小例子
PHP实例:批量删除文件夹及文件夹中的文件
php删除目录及所有文件的方法举例
php 目录遍历与删除的函数示例
php rmdir删除目录的三种方法
删除指定文件夹中所有文件的php代码
删除多级目录的php自定义函数
php删除目录与列出目录下所有文件的代码
php递归删除文件与目录的代码
php递归删除目录及多级子目录下所有文件的代码
php递归创建和删除文件夹的代码
php递归删除目录的例子

您可能感兴趣的文章:
php无限遍历目录代码
PHP删除N分钟前创建的所有文件的小例子
php删除目录下N天前所有文件的代码一例
php目录遍历与删除的代码一例
php遍历目录与其下所有文件
php 读取目录文件夹列表的例子
php遍历目录下所有文件和子文件夹的代码
删除指定文件夹中所有文件的php代码
最简单删除目录与文件php代码
Php删除指定文件与文件夹的方法

关键词: php遍历目录   
[关闭]