phpformsrmdirdelete-directory

how to delete directory php


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>

Solution

  • I found the mistake in your script. Here is the answer, hopefully this will solve it (if there is not another mistake somewhere):

    Your form is sent using method="post": <form method='post' action='/category'>

    In your code you check against $_GET:

    Wrong: if(isset($_GET['category_to_delete']) && $_GET['category_to_delete']!=''){

    Correct: if(isset($_POST['category_to_delete']) && $_POST['category_to_delete']!=''){

    Final Code must look like:

    if(isset($_POST['delete'])){
        if(isset($_POST['category_to_delete']) && $_POST['category_to_delete']!=''){
                $category_delete = $_POST['category_to_delete'];
                delete_directory('images/'.$category_delete);
                echo "Deleted";
        }
    }
    

    Also note that you check isset($_POST['delete']) -> its your button. If you dont click the button and press Enter to send the <form> there is no $_POST['delete'] and it won't work.

    One more info for you:

    Your function delete_directory and your form processing if(isset($_POST['delete'])){ is placed after your foreach(glob(dirname(__FILE__) . '/images/*') as $categories_list){

    This means, if you want to delete something and send the form to do so. The dropdown/select will contain the files again, because the files in directory images/* will be scanned again before you really process the form (the deletion of your selected files/directory). You should put the function and the processing of the form before the category. Usually things like this will be done before any HTML/Output Processing.