note 46299 deleted from function.rmdir by tomsommer
| From: | tomsommer@php.net | Date: | Wed, 06 Oct 2004 19:47:16 +0000 |
| Subject: | note 46299 deleted from function.rmdir by tomsommer | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-78093@lists.php.net to get a copy of this message | ||
Note Submitter: zeus_ado at yahoo dot com
----
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);
?>