Recursively deletes directories and files in rrmdir php

  • 2020-05-07 19:18:57
  • OfStack

 
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
(PHP 4, PHP 5)
rmdir - delete directories
Report a bug instructions
bool rmdir ( string $dirname )
Try to delete the directory specified by dirname. The directory must be empty and have permissions. Returns TRUE on success or FALSE on failure.
Note: rmdir() is also available for some URL packaging protocols, starting with PHP 5.0.0. See the list of Supported Protocols and Wrappers to see which URL packaging protocols rmdir() supports.
Note: added support for context (Context) in PHP 5.0.0. For a description of the context (Context), see the Stream function.
Note: when security mode is enabled, PHP checks when the script is executed to see if the directory being scripted has the same UID (owner) as the script being executed.
See mkdir() and unlink().
 
<?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 isn't my code, but just thought I would share, since it took me so long to find. This is a function to delete a folder, all sub-folders, and files in one clean move.
Just tell it what directory you want deleted, in relation to the page that this function is executed. Then set $empty = true if you want the folder just emptied, but not deleted. If you set $empty = false, or just simply leave it out, the given directory will be deleted, as well.
 

< ?php
function deleteAll($directory, $empty = false) {
if(substr($directory,-1) == "/") {
$directory = substr($directory,0,-1);
}
if(!file_exists($directory) || !is_dir($directory)) {
return false;
} elseif(!is_readable($directory)) {
return false;
} else {
$directoryHandle = opendir($directory);
while ($contents = readdir($directoryHandle)) {
if($contents != '.' && $contents != '..') {
$path = $directory . "/" . $contents;
if(is_dir($path)) {
deleteAll($path);
} else {
unlink($path);
}
}
}
closedir($directoryHandle);
if($empty == false) {
if(!rmdir($directory)) {
return false;
}
}
return true;
}
}
? >
[/code]
A patch to previous script to make sure rights for deletion is set:
 
<?php 
//Delete folder function 
function deleteDirectory($dir) { 
if (!file_exists($dir)) return true; 
if (!is_dir($dir) || is_link($dir)) return unlink($dir); 
foreach (scandir($dir) as $item) { 
if ($item == '.' || $item == '..') continue; 
if (!deleteDirectory($dir . "/" . $item)) { 
chmod($dir . "/" . $item, 0777); 
if (!deleteDirectory($dir . "/" . $item)) return false; 
}; 
} 
return rmdir($dir); 
} 
?> 

More instructions can reference http: / / cn php. net/rmdir

Related articles: