php批量删除指定文件夹中指定后缀名文件

这是一个比较实现的函数我们可以自定义要删除那些目录保存在一个数据中,然后我们根据数据进行遍历并且删除目录下指定文件了.

下面的例子我们是以删除指定目录的html为例子,如果你要其它文件我们只要把html改成你自己要删除的后缀名就可以了,代码如下:

  1. <?php
  2. $lanmu = array("action","tiyu","yizhi","sheji","gaoxiao","maoxian","qipai","celue","minjie","xiuxian","ertong","jingying","webgame");
  3. foreach($lanmu as $v2){
  4. $mstdel[] =glob(getcwd().DIRECTORY_SEPARATOR.$v2.DIRECTORY_SEPARATOR.'[1-9]*.html');
  5. }
  6. //var_dump($mstdel);
  7. //var_dump(count($mstdel));
  8. $i=0;
  9. foreach($mstdel as $val){
  10. foreach($val as $val2){
  11. //echo $val2."<Br>";
  12. if(unlink($val2)) $i++;
  13. //$countval2[] = $val2;
  14. }
  15. }
  16. echo 'del success: '.$i;
  17. //echo count($countval2);
  18. //var_dump(glob(getcwd().DIRECTORY_SEPARATOR.'sheji'.DIRECTORY_SEPARATOR.'[1-9]*.html'));
  19. //var_dump(glob('./sheji/[1-9]*.html'));
  20. //var_dump(glob('./sheji/[^index]*.html'));
  21. //array_walk(glob(getcwd().'/sheji/[1-9]*.html'),'unlink');
  22. //array_walk(glob('./sheji/[1-9]*.html'),'unlink');
  23. //array_walk(glob(getcwd().DIRECTORY_SEPARATOR.'sheji'.DIRECTORY_SEPARATOR.'[1-9]*.html'),'unlink');
  24. //var_dump(DIRECTORY_SEPARATOR);
  25. //var_dump(unlink('D:shanmaowwwwwwroottiantangtvxyxh4cc/sheji/20312.html'));
  26. ?>