An example of PHP directory traversal and deletion code

WBOY
Release: 2016-07-25 09:00:26
Original
1067 people have browsed it
The code for directory or folder traversal and deletion of specified files implemented in PHP is very simple and suitable for reference by beginners.

The main functions are as follows: Traverse the files, directories and subdirectories under the folder, read the directories and files under the current file, and delete the directories, subdirectories and files under the current folder. Note: Chinese catalogs are not supported yet.

<?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://bbs.it-home.org
* 删除目录下的文件以及子目录
* #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);
}
?>
Copy after login
Articles you may be interested in: How to delete specified files and folders in Php Small example of PHP deleting all files created N minutes ago php example: batch delete folders and files in folders Example of how to delete a directory and all files in php PHP directory traversal and deletion function examples Three ways to delete a directory using php rmdir Delete the php code of all files in the specified folder Delete php custom function for multi-level directories php code to delete a directory and list all files in the directory php code for recursively deleting files and directories php code for recursively deleting all files in a directory and multi-level subdirectories php code for recursively creating and deleting folders Example of php recursively deleting directories


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template