在PHP中,我如何递归地删除所有不为空的文件夹? [英] In PHP how do I recursively remove all folders that aren't empty?

查看:141
本文介绍了在PHP中,我如何递归地删除所有不为空的文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

如何在PHP中递归地删除目录及其整个内容(文件+子目录)?

我需要递归删除目录和不为空的子目录。我找不到任何有用的类或函数来解决此问题。

I need to recursively delete a directory and subdirectories that aren't empty. I can't find any useful class or function to solve this problem.

在此先感谢您的回答。

推荐答案

摘自官方文档中的第一条评论。

From the first comment in the official documentation.


http://php.net/manual/en/function.rmdir.php



<?php

 // When the directory is not empty:
 function rrmdir($dir) {
   if (is_dir($dir)) {
     $objects = scandir($dir);
     foreach ($objects as $object) {
       if ($object != "." && $object != "..") {
         if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object);
       }
     }
     reset($objects);
     rmdir($dir);
   }
 }

?>

将rmdir修改为rrmdir,以纠正明显的错字,从而创建递归函数。

Edited rmdir to rrmdir to correct typo from obvious intent to create recursive function.

这篇关于在PHP中,我如何递归地删除所有不为空的文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆