Home >php教程 >PHP源码 >A class encapsulated in PHP to display/delete all files and empty directories in the file directory. At the same time, the directory or directory and file with the file name 0 can be deleted.

A class encapsulated in PHP to display/delete all files and empty directories in the file directory. At the same time, the directory or directory and file with the file name 0 can be deleted.

大家讲道理
大家讲道理Original
2016-11-08 11:48:081185browse

<?php
class file{
    static public function ShowDir($path,$level=0){
        $dir=opendir($path);
        while(($file=readdir($dir)) !==false){
            if($file!=&#39;.&#39; && $file!=&#39;..&#39;){
                $filepath=$path.&#39;/&#39;.$file;
                for($i=0;$i<$level*5;$i++){
                    echo " ";
                }
                echo $file.&#39;<br>&#39;;
                if(is_dir($filepath)) {
                static::ShowDir($filepath, $level + 1);
                }
            }
        }
        closedir($dir);
    }
    static public function DeleteDir($path){
        $dir=opendir($path);
        while(($file=readdir($dir)) !== false){
            if($file!=&#39;.&#39; && $file!=&#39;..&#39;){
                $filepath=$path.&#39;/&#39;.$file;
                if(is_dir($filepath)){
                    self::DeleteDir($filepath);
                }else{
                    unlink($filepath);
                }
            }
        }
        closedir($dir);
        rmdir($path);
    }
}
?>

Statement:
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