I tried both answers, they did solve a problem I had with the code but it did not solve the issue I was having in which the parent directory was being deleted along with the selected items.
After looking and changing the code for some time I finally realized that the problem wasn't in the recursiveDeleteDirectory()
function but in the delete_file()
function itself.
The $selected_items
variable gets a concatenated string of the names of the items to be selected, it trims any unnecessary whitespaces and makes an array from the string. The problem was that somehow $selected_items got an additional empty entry which meant that I was then executing recursiveRemoveDirectory("{$directory}/");
instead of recursiveRemoveDirectory("{$directory}/{$item}");
.
I solved all this by adding:
if (empty($item)){
continue;
}
Here is how the code looks like:
function delete_file (){
global $directory;
$selected_items = explode(";", trim($_POST['selected']));
if (count($selected_items) < 1){
echo "0";
}
else {
foreach ($selected_items as $item){
if (empty($item)){
continue;
}
if (is_dir("{$directory}/{$item}")){
recursiveRemoveDirectory("{$directory}/{$item}");
}
else if (is_file("{$directory}/{$item}")){
unlink("{$directory}/{$item}");
}
}
echo "1";
}
}
function recursiveRemoveDirectory($rm_directory){
$files = file_explore();
if(count($files) > 0){
foreach($files as $file){
if(is_dir("{$rm_directory}/{$file}")) {
recursiveRemoveDirectory("{$rm_directory}/{$file}");
}
else if (is_file("{$rm_directory}/{$file}")) {
unlink("{$rm_directory}/{$file}");
}
}
}
rmdir($rm_directory);
Now my problem, which isn't really a stack overflow problem is the naming conversions of php functions, I never used underscore on function names but recently that have changed and I feel that's a bad habit, if someone wants to comment about that, you are free to do so, I really want to know how things must be done in php.
Cheers!