deleting a folder thats not empty
Posted: Thu Dec 21, 2006 2:41 pm
is something like this possible in php? i know i can delete an empty folder but how do i delete one that has contents?
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
foreach( new RecursiveIteratorIterator( new RecursiveDirectoryIterator('my/path/') ) as $file )
{
$file->delete() ;
}Code: Select all
rm -r /path/to/directory/Code: Select all
<?php
function remove_directory($dir) {
if ($handle = opendir("$dir")) {
while (false !== ($item = readdir($handle))) {
if ($item != "." && $item != "..") {
if (is_dir("$dir/$item")) {
remove_directory("$dir/$item");
} else {
unlink("$dir/$item");
echo " removing $dir/$item<br>\n";
}
}
}
closedir($handle);
rmdir($dir);
echo "removing $dir<br>\n";
}
}
remove_directory("/path/to/dir");
?>Code: Select all
$dirContents = opendir("directoryPath");
$i = 0;
while ($files = readdir($dirContents)) {
$contents[$i] = $files;
$i++;
}
if (count($contents) <= 2) {
// This will run if the directory is empty.
}
else {
// This will run if the there are files in the directory
}Code: Select all
print_r($contents);Code: Select all
if (count($contents) <= 2) {