我正在尝试制作表格以删除images
目录中的子目录中的选定选项。我有一个函数可以删除子文件夹中的所有文件,然后删除整个子文件夹,但是当我调用它时,什么也没有发生。有人能帮我吗?这是我的 html 表单和 php 代码:
<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>