7
Using my example below, I can delete a folder and the files contained in it:
$uploaddir = "../img/uploads/".$destino_sa."/";
$dir_contents = scandir($uploaddir);
if(is_dir($uploaddir)) {
foreach($dir_contents as $content) {
unlink($uploaddir.'/'.$content);
rmdir($uploaddir);
}
}
However, by using the same example to delete a folder that contains a subfolder and its files, I am not successful:
$uploaddir2 = "../img/uploads/hoteis/".$id_destino2."/".$destino_sa2."/";
$dir_contents2 = scandir($uploaddir);
if(is_dir($uploaddir2)) {
foreach($dir_contents2 as $content2) {
unlink($uploaddir2.'/'.$content2);
rmdir($uploaddir2);
}
}
Then you see that $destino
and $destino2
are completely different locations, being the last one, which is causing me problems at the time of being removed. Here is an example of what I want to remove through $destino2
:
../img/uploads/hoteis/3/hotel_emiliano/
- Briefcase hotel_emiliano and its content
- Briefcase 3 and its contents, in this case the folder
hotel_emiliano
How should I proceed?
Although it’s more performative, I don’t recommend using functions like
exec()
oreval()
in simple cases, for safety reasons.– gmsantos
Concern for safety independent of the complexity of the case.
– Daniel Omine