简体   繁体   English

会递归删除与特定文件扩展名匹配的文件吗?

[英]Deleting files matching a specific file extension recursivly?

I would like to delete all files matching a particular extension in a specified directory and all subtree. 我想删除与指定目录和所有子树中的特定扩展名匹配的所有文件。 I suppose I should be using using unlink but some help would be highly appreciated... Thank you! 我想我应该使用unlink,但是会非常感谢一些帮助...谢谢!

you need a combination of this 您需要结合使用

Recursive File Search (PHP) 递归文件搜索(PHP)

And the unlink / delete unlink / delete

You should be able to edit the example instead of echoing the file, to delete it 您应该能够编辑示例而不是回显文件,然后将其删除

To delete specific extension files from sub directories, you can use the following function. 要从子目录中删除特定的扩展名文件,可以使用以下功能。 Example: 例:

<?php 
function delete_recursively_($path,$match){
   static $deleted = 0,
   $dsize = 0;
   $dirs = glob($path."*");
   $files = glob($path.$match);
   foreach($files as $file){
      if(is_file($file)){
         $deleted_size += filesize($file);
         unlink($file);
         $deleted++;
      }
   }
   foreach($dirs as $dir){
      if(is_dir($dir)){
         $dir = basename($dir) . "/";
         delete_recursively_($path.$dir,$match);
      }
   }
   return "$deleted files deleted with a total size of $deleted_size bytes";
}
?>

eg To remove all text files you can use it as follows: 例如,要删除所有文本文件,可以按以下方式使用它:

<?php echo delete_recursively_('/home/username/directory/', '.txt'); ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM