note 70909 deleted from function.rmdir by aidan
| From: | aidan@php.net | Date: | Wed, 15 Apr 2009 04:25:27 +0000 |
| Subject: | note 70909 deleted from function.rmdir by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-153261@lists.php.net to get a copy of this message | ||
Note Submitter: sarangan dot thuraisingham at gmail dot com
----
I installed Joomla CMS and the files where under apache user. So I couldn't delete from my SSH
login. Your little function helped. But, you forgot about hidden files in Unix. Filenames beginning
with a . are not returned in the glob list. So the modified code looks like this:
<?php
function removeDir($path) {
// Add trailing slash to $path if one is not there
if (substr($path, -1, 1) != "/") {
$path .= "/";
}
$normal_files = glob($path . "*");
$hidden_files = glob($path . "\.?*");
$all_files = array_merge($normal_files, $hidden_files);
foreach ($all_files as $file) {
# Skip pseudo links to current and parent dirs (./ and ../).
if (preg_match("/(\.|\.\.)$/", $file))
{
continue;
}
if (is_file($file) === TRUE) {
// Remove each file in this Directory
unlink($file);
echo "Removed File: " . $file . "<br>";
}
else if (is_dir($file) === TRUE) {
// If this Directory contains a Subdirectory, run this Function on it
removeDir($file);
}
}
// Remove Directory once Files have been removed (If Exists)
if (is_dir($path) === TRUE) {
rmdir($path);
echo "<br>Removed Directory: " . $path . "<br><br>";
}
}
#To remove a dir:
removeDir('/home/fhlinux206/t/thuraisingham.net/tmp/');
?>