This function is useful to delete all inner folder of given path of folder.
Lets explain in example:
if you have folder which name is user_1 and user_1 have lots of folder like (my_album, my_profile,friends_photo etc.)
if you want to delete folder (my_album, my_profile,friends_photo etc.) call function like belew
<?php
deleteDirectory($dir); // $dir = path of user_1 folder
?>
function deleteDirectory($dir) {
if (!file_exists($dir)) return true;
if (!is_dir($dir) || is_link($dir)) return unlink($dir);
foreach (scandir($dir) as $item) {
if ($item == '.' || $item == '..') continue;
if (!deleteDirectory($dir . "/" . $item)) {
chmod($dir . "/" . $item, 0777);
if (!deleteDirectory($dir . "/" . $item)) return false;
};
}
return rmdir($dir);
}
cheers!
Lets explain in example:
if you have folder which name is user_1 and user_1 have lots of folder like (my_album, my_profile,friends_photo etc.)
if you want to delete folder (my_album, my_profile,friends_photo etc.) call function like belew
<?php
deleteDirectory($dir); // $dir = path of user_1 folder
?>
function deleteDirectory($dir) {
if (!file_exists($dir)) return true;
if (!is_dir($dir) || is_link($dir)) return unlink($dir);
foreach (scandir($dir) as $item) {
if ($item == '.' || $item == '..') continue;
if (!deleteDirectory($dir . "/" . $item)) {
chmod($dir . "/" . $item, 0777);
if (!deleteDirectory($dir . "/" . $item)) return false;
};
}
return rmdir($dir);
}
cheers!
No comments:
Post a Comment