note 61409 added to function.rmdir
| From: | bobbfwed at comcast dot net | Date: | Thu, 02 Feb 2006 19:20:03 +0000 |
| Subject: | note 61409 added to function.rmdir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-102883@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 64.71.164.2
Probable Submitter: 67.181.154.141
----
Manual Page -- http://www.php.net/manual/en/function.rmdir.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+61409
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+61409&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+61409&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+61409&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+61409&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+61409&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+61409&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+61409&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+61409&report=yes
Search -- http://master.php.net/manage/user-notes.php