简体   繁体   中英

How to delete all contents of a directory without deleting the directory

I have a directory on my server that I need to clear out using PHP. I need to delete all files and folders inside this directory, however, I do not want to delete the main directory itself. Everything I have read, examples I have found, they all seem to delete everything inside the directory given but then after it also deletes the given directory. One function I tried is below, but again it also deletes the main directory and I can't have that.

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

Get rid of this rmdir($dir); on the last part of your function.

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 line deletes the directory you specified after its content has been deleted
    }
}

Remove this line from your existing code rmdir($dir); , with this line your are also removing the parent directory that you passed on your function parameter. I've also added what code snippet I usually use for deleting contents from directory.

function remove_directory($str) {
 //It it's a file.
 if (is_file($str)) {
     //Attempt to delete it.
     return unlink($str);
 }
 //If it's a directory.
 elseif (is_dir($str)) {
    //Get a list of the files in this directory.
    $scan = glob(rtrim($str,'/').'/*');
    //Loop through the list of files.
    foreach($scan as $index=>$path) {
        //Call our recursive function.
        remove_directory($path);
    }
    //Remove the directory itself.
    return @rmdir($str);
  }
 }

//call our function
 $dir='directory_name';
 remove_directory($dir);

The version of the code I posted before also deleted subdirectories, so removing the last rmdir in the code would stop it from doing that. However, I was able to make changes to the code that works. This code will delete all contents inside the given directory including files and folders without deleting the given directory.

function rrmdir($dir, $issub = false) {
    if (is_dir($dir)) {
        $objects = scandir($dir);
        foreach ($objects as $object) {
            if ($object != "." && $object != "..") {
                if (filetype($dir."/".$object) == "dir")
                     rrmdir($dir."/".$object, true);
                else unlink   ($dir."/".$object);
            }
        }
        reset($objects);
        if($issub) {
          rmdir($dir);
        }
    }
}

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