简体   繁体   中英

delete all subdirectories within a specificied directory in PHP?

How could I delete all subdirectories within a specified directory?

The directory is 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).

rmdir() only removes the last directory in the given path...so I'm guessing i'd have to loop? :/

All help appreciated.

(PS: the subdirectories don't contain any files)

Taken from the PHP manual entry for rmdir :

 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

For example, the following command will remove directory C:\blah and all subdirectories and files contained therein. No prompt will be displayed.

RMDIR c:\blah /s /q

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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