Home  >  Article  >  Backend Development  >  PHP method to delete related files in a specified directory, _PHP tutorial

PHP method to delete related files in a specified directory, _PHP tutorial

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

php implements the method of deleting related files in a specified directory,

The example in this article describes the method of deleting related files in the specified directory in PHP. Share it with everyone for your reference. The specific implementation method is as follows:

Generally speaking, the easiest way to delete files in PHP is to directly use the unlink command. If you need to delete the specified file in the specified directory, you need to traverse and delete it.

The specific example code is as follows:

Copy code The code is as follows:
//Delete illegal files in the specified folder
function my_del($dir)
{
If(is_dir($dir)){
//Open the specified folder
If($handle = opendir($dir))
           {
​​​​​​while(false !== ($file = readdir($handle)))
                {
If($file !== '.' && $file !== '..')
                                                   {
my_del($dir.'/'.$file);
                 }
            }
               $res = closedir($handle);
}
}else{
​​​​ //Delete all files except pictures
                  $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')){
//Filter by name
                    @unlink($dir);
         } else {
//Filter by the actual content of the file
                    $info = @getimagesize($dir);
If(!$info || $info[2] !=2) {
                               @unlink($dir);
                }
}
}
}
$dir='D:/xampp/htdocs/www/avatar001/12/47/';
my_del($dir);

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/897018.htmlTechArticlephp method to delete related files in the specified directory. This article describes the example of php to delete related files in the specified directory. method. Share it with everyone for your reference. The specific implementation methods are as follows...
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