Hi
I have a function to delete a folder and all contents including subfolders and files. This works ok, here is code :-
function delete_folder_and_contents($dir) {
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (filetype($dir."/".$object) == "dir")
delete_folder_and_contents($dir."/".$object);
else unlink ($dir."/".$object);
}
}
reset($objects);
if (! rmdir($dir)){
echo '<div class = "warning">Could not delete existing content, please ensure no other programs or
utilities are accessing the folder or contents - <strong>' . $dir . '</strong></div><br>';
}
}
}
However I really want to use this function in 3 ways -
1 - Delete folder and contents (including subfolders and files)
2 - Just delete contents (subfolders and files but leave original folder)
3 - either of the above but ignore (do not delete ONE designated subfolder)
I have been trying and struggling for hours now to amend this function to accept three parameters instead of one to do the following:-
- first parameter - is the folder name (this works ok) and deletes folder and all subfolders and files
- second parameter - delete root/parent folder yes/no - by this I mean option to delete designated folder and contents or just contents (subfolders and files but leave the root/designated folder)
- third parameter - subfolder to ignore (including subfolder contents)
For example -
delete_folder_and_contents($root, $delete_root, $ignore )
so :-
delete_folder_and_contents('main', 'yes', '' )
would delete the folder main and all contents
delete_folder_and_contents('main', 'no', '')
would delete the contents of the folder āmainā but keep the empty folder
delete_folder_and_contents('main', 'no', 'child')
would delete the folder contents, keep the empty folder and the subfolder āchildā and subfolder contents.
Basically I need to be able to delete entire folder or just delete contents of folder or just delete folder contents and skip/ignore 1 subfolder.
I dont actually mind if I have to have 3 different functions, but all in one would be great.
Any advice or ideas eagerly accepted, even advice on original function structure.
Thanks guys