I'm trying to make form to delete chosen option which is subdirectory in images
directory. I have a function which removes all files in subfolder and then a whole subfolder but when I call it, nothing happens. Can someone help me?
Here's my html form and php code:
<form method='post' action='/category'>
<div class="form-group">
<select class="custom-select" name="category_to_delete" style="width:60%;">
<?php
foreach(glob(dirname(__FILE__) . '/images/*') as $categories_list){
if(is_dir($categories_list)){
$categories_list = basename($categories_list);
echo "<option value='" . $categories_list . "'>".$categories_list."</option>";
}
}
?>
</select>
</div>
<?php
function delete_directory($dirname) {
if (is_dir($dirname))
$dir_handle = opendir($dirname);
if (!$dir_handle)
return false;
while($file = readdir($dir_handle)) {
if ($file != "." && $file != "..") {
if (!is_dir($dirname."/".$file))
unlink($dirname."/".$file);
else
delete_directory($dirname.'/'.$file);
}
}
closedir($dir_handle);
rmdir($dirname);
return true;
}
if(isset($_POST['delete'])){
if(isset($_GET['category_to_delete']) && $_GET['category_to_delete']!=''){
$category_delete = $_POST['category_to_delete'];
delete_directory('images/'.$category_delete);
echo "Deleted";
}
}
?>
<div class="form-group">
<button type='submit' value='Delete' name='delete'> Usuń</button>
</div>
</form>