note 46299 added to function.rmdir
| From: | zeus_ado at yahoo dot com | Date: | Wed, 06 Oct 2004 06:46:26 +0000 |
| Subject: | note 46299 added to function.rmdir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-78058@lists.php.net to get a copy of this message | ||
this is just a script to remove a directory and all it subfolders all what should be done is to
specify the path in "$todelet" , the script will get the mother path for you , change the
defaut path of php to this path and then proceed with deleting the directory and evrything in it.
<?php
$todelet="/www/htdocs/tmp/tmp1";
/*this function will return the mother directory to be able to delete the son*/
function getparent($son){
$place=strrpos($son,'/');//will search for the occurence / in the string
return substr($son,0,$place+1);//return substring 1 is added to the length because if not it will
not return the /
}
//check if a directory is empty
function isempty($direct){
$handle=dir($direct);
$counter=0;
//a directory is empty if it contains the '.' and '..' pointers if the directory
contains just 1 file break the operation and return true;
while(false!== ($tmp=$handle->read())){
if($tmp!="." && $tmp!="..")
$counter++;
if($counter==1)
break;
}
$handle->close();
return $counter==0;
}
/*called by delete and will delete evrything*/
function sub_delete($todelete){
$handle=dir($todelete);
echo "$handle->path<BR>";
//strating to delete the directory after opening it
while(false!==($tmp=$handle->read()))
{
echo "$todelete/$tmp<BR>";
if($tmp!="." && $tmp!="..")//dosen t take count of those pointers
{
if(is_file("$todelete/$tmp")) // if it s a file delete directly nothing to be done
{
echo "$todelete/$tmp";
unlink("$todelete/$tmp");
}
else if(isempty("$todelete/$tmp"))// if it s an empty directory also delete it directly
rmdir("$todelete/$tmp");
else
sub_delete("$todelete/$tmp");//if not empty open the directory and delete all what s in
it
}
}
$handle->close();
rmdir($todelete);//delete the mother directory
}
/*called to find the mother directory changed the path to the mother to be able to delete the son or
else we can t*/
function delete($direct){
$mother=getparent($direct);
chdir($mother);
sub_delete($direct);
}
delete ($todelet);
?>
----
Manual Page -- http://www.php.net/manual/en/function.rmdir.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+46299
Delete -- http://master.php.net/manage/user-notes.php?action=delete+46299&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+46299&report=yes
Search -- http://master.php.net/manage/user-notes.php