I was working on my PHP Tutorial and I was working on file management functions in PHP. PHP has a function called rmdir() that will remove an empty directory for you but it can not delete a non-empty folder. I was board so I just wrote this and tested it on my server It will recursively go into each sub-directory delete all the files and folders hope it is helpful to you:
php
/* Sudo Code for the function
if there isn’t any dirname mentioned or the name is not a directory {just end the script
} otherwise{
get all the files and folders in the directory
moves into the directory
for each file and folder do this check{
as long as it is not . and .. (the standard folders) then do this:{
if it is a folder{
call this function to delete it
}otherwise{
delete the file
}
}
}
we moved into this folder before so now we move back up one level
delete the folder
}
*/
function remove_dir($name){
if($name==”” or !file_exists($name)){
echo “I need something to delete!<br />”;
die;
}//—————end of if
$ars=scandir($name);
chdir($name);
foreach($ars as $ar){
if($ar!=”.” and $ar!=”..”){
if(is_dir($ar)){
echo “found directory $ar <br />”;
remove_dir($ar);
}else{
echo “found a file $ar <br />”;
unlink($ar);
}//——————–end of if
}//——————-end of if
}//—————-end of foreach
chdir(“..”);
rmdir($name);
}//—————end of function
remove_dir(Directory name here);// put any directory name here and it will delete it for you
?>
Tell me how to improve it or if you have any recommendations for this code
Sorry about that, It’s fix now
let me know what you think.