简体   繁体   English

删除 PHP 中指定目录中的所有子目录?

[英]delete all subdirectories within a specificied directory in PHP?

How could I delete all subdirectories within a specified directory?如何删除指定目录中的所有子目录?

The directory is c:/files/目录为c:/files/

and I want to delete all subdirectories within their (example):我想删除它们(示例)中的所有子目录:

c:/files/something/something/something/

c:/files/another-something/

So in the end c:/files/ just remains (is empty and has no subdirectories).所以最后 c:/files/ 仍然存在(为空且没有子目录)。

rmdir() only removes the last directory in the given path...so I'm guessing i'd have to loop? rmdir()只删除给定路径中的最后一个目录......所以我猜我必须循环? :/ :/

All help appreciated.所有帮助表示赞赏。

(PS: the subdirectories don't contain any files) (PS:子目录不包含任何文件)

Taken from the PHP manual entry for rmdir :取自rmdir 的 PHP 手册条目

 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); 
   } 
 }

This solves the problem using recursion.这使用递归解决了这个问题。

I think you are looking for RMDIR /S我认为您正在寻找RMDIR /S

For example, the following command will remove directory C:\blah and all subdirectories and files contained therein.例如,以下命令将删除目录C:\blah以及其中包含的所有子目录和文件。 No prompt will be displayed.不会显示任何提示。

RMDIR c:\blah /s /q

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

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