Home  >  Article  >  Backend Development  >  Example of deleting related files in a specified directory with php_PHP tutorial

Example of deleting related files in a specified directory with php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:16:47915browse

php删除指定目录下的相关文件实例

 

代码如下  


代码如下  


 //删除指定文件夹下的非法文件

 function my_del($dir)
 {
    if(is_dir($dir)){
        //打开指定文件夹
        if($handle = opendir($dir))
        {
            while(false !== ($file = readdir($handle)))
            {
                if($file !== '.' && $file !== '..')
                 {
                        my_del($dir.'/'.$file);
                 }
            }
            $res = closedir($handle);
        }
    }else{
        //删掉除图片意外的所有文件
        $avatararr = array('180x180.jpg', '30x30.jpg', '45x45.jpg', '90x90.jpg');
        $ex = explode('/', $dir);
        $endex = end($ex);
        if((strripos($endex,'.jpg') === false) || (substr($endex, -4) != '.jpg')){
                    //按名称过滤
                @unlink($dir);
        } else {
                    //按文件实质内容过滤
                $info = @getimagesize($dir);
                if(!$info || $info[2] !=2) {
                        @unlink($dir);
                }
        }
    }
 }

 $dir='D:/xampp/htdocs/www/avatar001/12/47/';
 my_del($dir);

 //删除指定文件夹下的非法文件

 function my_del($dir)
 {
    if(is_dir($dir)){
        //打开指定文件夹
        if($handle = opendir($dir))
        {
            while(false !== ($file = readdir($handle)))
            {
                if($file !== '.' && $file !== '..')
                 {
                        my_del($dir.'/'.$file);
                 }
            }
            $res = closedir($handle);
        }
    }else{
        //删掉除图片意外的所有文件
        $avatararr = array('180x180.jpg', '30x30.jpg', '45x45.jpg', '90x90.jpg');
        $ex = explode('/', $dir);
        $endex = end($ex);
        if((strripos($endex,'.jpg') === false) || (substr($endex, -4) != '.jpg')){
                    //按名称过滤
                @unlink($dir);
        } else {
                    //按文件实质内容过滤
                $info = @getimagesize($dir);
                if(!$info || $info[2] !=2) {
                        @unlink($dir);
                }
        }
    }
 }

 $dir='D:/xampp/htdocs/www/avatar001/12/47/';
 my_del($dir);

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/895549.htmlTechArticlephp Delete related files in the specified directory The example code is as follows?php //Delete illegal files in the specified folder function my_del($dir) { if(is_dir($dir)){ //Open the specified folder if($hand...
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