PHP: Simplest way to delete a folder (including its contents)
Asked Answered
T

7

33

The rmdir() function fails if the folder contains any files. I can loop through all of the the files in the directory with something like this:

foreach (scandir($dir) as $item) {
    if ($item == '.' || $item == '..') continue;
    unlink($dir.DIRECTORY_SEPARATOR.$item);
}
rmdir($dir);

Is there any way to just delete it all at once?

Thousand answered 18/8, 2009 at 21:38 Comment(0)
P
47

Well, there's always

system('/bin/rm -rf ' . escapeshellarg($dir));

where available.

Paganism answered 18/8, 2009 at 21:42 Comment(2)
@KashifRaza: Right. Hence "where available".Paganism
On Windows use this: system('rd /Q /S "' . $dir . '"');Burnet
Z
63

rrmdir() -- recursively delete directories:

function rrmdir($dir) { 
  foreach(glob($dir . '/*') as $file) { 
    if(is_dir($file)) rrmdir($file); else unlink($file); 
  } rmdir($dir); 
}
Zurek answered 21/11, 2012 at 10:15 Comment(6)
I once delete my whole project using such function, be careful!Griddlecake
@gskema, Your comment made me laugh!!! I can only imagine the "ohhhh shhiiiiittttt" moment. I hope you got it all back!Passade
@Passade Actually, I didn't. When unlink deletes something, it means it. Good thing that it was only a test project. Lesson of the day: use a remote VCSGriddlecake
@gskema, I sympathize. I literally just deleted my entire website by accident. Thank god I've got a backup.Autotoxin
@TricksfortheWeb Your name checks out :DGriddlecake
delete whole project folder: rrmdir('.'); :)Wateriness
P
47

Well, there's always

system('/bin/rm -rf ' . escapeshellarg($dir));

where available.

Paganism answered 18/8, 2009 at 21:42 Comment(2)
@KashifRaza: Right. Hence "where available".Paganism
On Windows use this: system('rd /Q /S "' . $dir . '"');Burnet
S
3
function delete_files($dir) {
  if (is_dir($dir)) {
    $objects = scandir($dir);
    foreach ($objects as $object) {
      if ($object != "." && $object != "..") {
        if (filetype($dir."/".$object) == "dir") 
           delete_files($dir."/".$object); 
        else unlink   ($dir."/".$object);
      }
    }
    reset($objects);
    rmdir($dir);
  }
 }
Starofbethlehem answered 21/1, 2015 at 10:8 Comment(0)
G
2

As per this source;

Save some time, if you want to clean a directory or delete it and you're on windows.

Use This:

    chdir ($file_system_path);
    exec ("del *.* /s /q");

You can use other DEL syntax, or any other shell util. You may have to allow the service to interact with the desktop, as that's my current setting and I'm not changing it to test this.

Else you could find an alternative method here.

Glyconeogenesis answered 18/8, 2009 at 21:51 Comment(1)
it's a bad idea to have to depend on your OS... this only works on Windows and would fail on a Unix system for instance...Lollapalooza
I
1

Try this :

exec('rm -rf '.$user_dir);
Isoprene answered 2/1, 2013 at 11:22 Comment(2)
never ever! you must esape $user_dir! please add escapeshellarg($user_dir)Kraemer
unsafe and worse: depends on your OS... this would fail on a windows system for instance...Lollapalooza
M
1

This fuction delete the directory and all subdirectories and files:

function DelDir($target) {
    if(is_dir($target)) {
        $files = glob( $target . '*', GLOB_MARK ); //GLOB_MARK adds a slash to directories returned

        foreach( $files as $file )
        {
            DelDir( $file );      
        }

        rmdir( $target );
    } elseif(is_file($target)) {
        unlink( $target );  
    }
}
Mister answered 25/3, 2015 at 23:2 Comment(0)
B
0

One safe and good function located in php comments by lprent It prevents accidentally deleting contents of symbolic links directories located in current directory

public static function delTree($dir) { 
   $files = array_diff(scandir($dir), array('.','..')); 
    foreach ($files as $file) { 
      (is_dir("$dir/$file") && !is_link($dir)) ? delTree("$dir/$file") : unlink("$dir/$file"); 
    } 
    return rmdir($dir); 
  } 
Bazaar answered 13/9, 2015 at 21:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.