note 61409 deleted from function.rmdir by aidan
| From: | aidan@php.net | Date: | Wed, 15 Apr 2009 04:27:57 +0000 |
| Subject: | note 61409 deleted from function.rmdir by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-153266@lists.php.net to get a copy of this message | ||
Note Submitter: bobbfwed at comcast dot net
----
I have programmed a really nice program that remotely lets you manage files as if you have direct
access to them (http://sourceforge.net/projects/filemanage/). I have a bunch of really handy
functions to do just about anything to files or directories. In it I use this directory delete
function.
Here is the function I made; it will likely need tweaking to work as a standalone script, since it
relies of variables set by my program (eg: loc1 -- which dynamically changes in my program):
<?PHP
// loc1 is the path on the computer to the base directory that may be moved
define('loc1', 'C:/Program Files/Apache Group/Apache/htdocs', true);
// use this format:
$dir = '[reletive path]';
if(remdir($dir))
rmdir($dir);
else
echo 'There was an error.';
// this function deletes all files and sub directories directories in a directory
// bool remdir( str 'directory path' )
function remdir($dir){
if(!isset($GLOBALS['remerror']))
$GLOBALS['remerror'] = false;
if($handle = opendir(loc1 . $dir)){ // if the folder exploration is sucsessful, continue
while (false !== ($file = readdir($handle))){ // as long as storing the next file to $file is
successful, continue
$path = $dir . '/' . $file;
if(is_file(loc1 . $path)){
if(!unlink(loc1 . $path)){
echo '<u><font color="red">"' . $path . '"
could not be deleted. This may be due to a permissions problem.</u><br>Directory cannot
be deleted until all files are deleted.</font><br>';
$GLOBALS['remerror'] = true;
return false;
}
} else
if(is_dir(loc1 . $path) && substr($file, 0, 1) != '.'){
remdir($path);
@rmdir(loc1 . $path);
}
}
closedir($handle); // close the folder exploration
}
if(!$GLOBALS['remerror']) // if no errors occured, delete the now empty directory.
if(!rmdir(loc1 . $dir)){
echo '<b><font color="red">Could not remove directory "'
. $dir . '". This may be due to a permissions
problem.</font></b><br>';
return false;
} else
return true;
return false;
} // end of remdir()
?>
This new function will be in 0.9.7 (the current release of File Manage) which was just released.
Hope this helps some people.