简体   繁体   中英

php deleting a specific folder and all its contents

I'm using php to delete folders containing images of posts that where deleted. I'm using the code below which I found online and does a good job.

I want to know how can I delete only a specific folder in a folder when there are other folders in it.

When I using the code below, how is it possible to do this? Using: /dev/images/norman/8 -> Will not delete folder 8 Using: /dev/images/norman/ -> Will delete all folders

Eg:
/dev/images/norman/8 -> I need to delete only this folder
/dev/images/norman/9
/dev/images/norman/10
/dev/images/norman/11
/dev/images/norman/12

<?php
$path = $_SERVER['DOCUMENT_ROOT'].'/dev/images/norman/8';

emptyDir($path);

function emptyDir($path) { 

    // INITIALIZE THE DEBUG STRING
    $debugStr  = '';
    $debugStr .= "Deleting Contents Of: $path<br /><br />";

    // PARSE THE FOLDER
    if ($handle = opendir($path)) {

        while (false !== ($file = readdir($handle))) {

            if ($file != "." && $file != "..") {

                // IF IT"S A FILE THEN DELETE IT
                if(is_file($path."/".$file)) {

                    if(unlink($path."/".$file)) {
                    $debugStr .= "Deleted File: ".$file."<br />";   
                    }

                } else {

                    // IT IS A DIRECTORY
                    // CRAWL THROUGH THE DIRECTORY AND DELETE IT'S CONTENTS

                    if($handle2 = opendir($path."/".$file)) {

                        while (false !== ($file2 = readdir($handle2))) {

                            if ($file2 != "." && $file2 != "..") {
                                if(unlink($path."/".$file."/".$file2)) {
                                $debugStr .= "Deleted File: $file/$file2<br />";    
                                }
                            }

                        }

                    }

                    if(rmdir($path."/".$file)) {
                    $debugStr .= "Directory: ".$file."<br />";  
                    }

                }

            }

        }

    }
    echo $debugStr;
}

?>
<?php

delete_directory($dirname) {
   if (is_dir($dirname))
      $dir_handle = opendir($dirname);
   if (!$dir_handle)
      return false;
   while($file = readdir($dir_handle)) {
      if ($file != "." && $file != "..") {
         if (!is_dir($dirname."/".$file))
            unlink($dirname."/".$file);
         else
            delete_directory($dirname.'/'.$file);    
      }
   }
   closedir($dir_handle);
   rmdir($dirname);
   return true;
 }
?>

if you are using, version 5.1 and above,

<?php
function deleteDir($dir) {
   $iterator = new RecursiveDirectoryIterator($dir);
   foreach (new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST) as $file) 
   {
      if ($file->isDir()) {
         rmdir($file->getPathname());
      } else {
         unlink($file->getPathname());
      }
   }
   rmdir($dir);
}

deleteDir("temporary");
?>

You want to hear about rmdir .

if(is_file($path."/".$file)) {

    if(unlink($path."/".$file)) {
    $debugStr .= "Deleted File: ".$file."<br />";   
    }

} else {

    if(rmdir($path."/".$file)) {
        $debugStr .= "Deleted Directory: ".$file."<br />";   
    }

}

EDIT: as rmdir can only handle empty dirs, you may use this solution as reported in rmdir's page comments:

function rrmdir($dir) {
    foreach(glob($dir . '/*') as $file) {
        if(is_dir($file))
            rrmdir($file);
        else
            unlink($file);
    }
    rmdir($dir);
}

It just recursively deletes everything in $dir , then gets rid of directory itself.

You can use system commands ex. exec("rm -rf {$dirPath}"); or if you want to do it by PHP you have to go recursive, loops won't do it right.

public function deleteDir($path) {

    if ($handle = opendir($path)) {

        while (false !== ($file = readdir($handle))) {

            if ($file != "." && $file != "..") {

                // IF IT"S A FILE THEN DELETE IT
                if(is_file($path."/".$file)) {

                    if(unlink($path."/".$file)) {
                        $debugStr .= "Deleted File: ".$file."<br />";
                    }

                } else {
                    deleteDir($path."/".$file."/");
                    rmdir($path."/".$file);
                }
            }
        }
    }
}

When I using the code below, how is it possible to do this? Using: /dev/images/norman/8 -> Will not delete folder 8 Using: /dev/images/norman/ -> Will delete all folders

I think your problem is that you're missing "/" at the end of "/dev/images/norman/8"

I added an $exclude param to your function, this param it's an array with the names of directories you want to exclude from being deleted, like so:

$path = $_SERVER['DOCUMENT_ROOT'].'/dev/images/norman/';

emptyDir($path); //will delete all under /norman/
emptyDir($path, array('8')); //will delete all under /norman/ except dir 8
emptyDir($path, array('8','10')); //will delete all under /norman/ except dir 8 and 10

function emptyDir($path,$exclude=false) { 

    // INITIALIZE THE DEBUG STRING
    $debugStr  = '';
    $debugStr .= "Deleting Contents Of: $path<br /><br />";
    if (!$exclude) {
       $exclude = array();
    }

    // PARSE THE FOLDER
    if ($handle = opendir($path)) {

        while (false !== ($file = readdir($handle))) {

            if ($file != "." && $file != "..") {

                // IF IT"S A FILE THEN DELETE IT
                if(is_file($path."/".$file)) {

                    if(unlink($path."/".$file)) {
                    $debugStr .= "Deleted File: ".$file."<br />";   
                    }

                } else if (!in_array($file, $exclude)) {

                    // IT IS A DIRECTORY
                    // CRAWL THROUGH THE DIRECTORY AND DELETE IT'S CONTENTS

                    if($handle2 = opendir($path."/".$file)) {

                        while (false !== ($file2 = readdir($handle2))) {

                            if ($file2 != "." && $file2 != "..") {
                                if(unlink($path."/".$file."/".$file2)) {
                                $debugStr .= "Deleted File: $file/$file2<br />";    
                                }
                            }

                        }

                    }

                    if(rmdir($path."/".$file)) {
                    $debugStr .= "Directory: ".$file."<br />";  
                    }

                }

            }

        }

    }
    echo $debugStr;
}
$path='./ggg';
rrmdir($path);
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);
}
}

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