-1

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>
cherryBom
  • 53
  • 1
  • 6
  • Does it delete files but not directories - or nothing happens? Do you receive any error messages? The code looks Ok. The question is if the files/directories have the rights so that the script can access (delete) them. – ChrisG Jun 08 '20 at 17:54
  • nothing happens, all files stays in folders and I don't have any errors on website or in console. files and directories have all rights – cherryBom Jun 08 '20 at 18:13

1 Answers1

0

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.

ChrisG
  • 202
  • 2
  • 13
  • Ah yes, thank you so much! I didn't notice this mistake. I corrected my code and now it works brilliant. Thank you :) – cherryBom Jun 08 '20 at 21:27
  • Yes this is such a simple mistake that its difficult to recognise because you dont calculate with such a mistake. I also had to take a second look. :-) – ChrisG Jun 08 '20 at 21:29