How do I finish writing in a delete function into this code to delete both directory and files in it?



  • So I have look through and directory exclude certain files, show files numbered with a delete button and need help to write in a delete function to delete the directory and the files in it and not sure how that is written can someone help out.

    <?php
        $dir = './';
        $files = scandir($dir);
        sort($files);
        $count = -1 ;
        foreach ($files as $file) {
            $v_delete = "delete_".$count;
                if ($file != '.' && $file != '..' && $file != 'read.php') {
                $str_URL = "./".$file;
                echo "<tr>";
                echo "<td>";
                echo $count;
                echo "</td>";
                echo "<td>";
                echo $file;
                echo "</td>";            
                echo "<td>";
                echo "<form action='' method='post'><input type='submit' value='Delete' name='".$v_delete."'/></form>";
                if(isset($_POST[$v_delete])) {
                // Your php delete code here
    
    
                echo "delete file : ".$file;
                }
                echo "</td>";
    
            }
            $count++;
        }
    ?>
    


  • Thank you, so that means with one more revision we can say delete all the files in a subfolder in those viewable directories called images.

    <?php
            $root_dir = './';
            $directories = scandir($root_dir);
            sort($directories);
            $count = -1 ;
            foreach ($directories as $dir) {
                $v_delete = "delete_".$count;
                if ($dir != '.' && $dir != '..' && $dir != 'index.php') {
                $str_URL = "./".$dir;
                echo "<tr>";
                echo "<td>";
                echo $count;
                echo "</td>";
                echo "<td>";
                echo $dir;
                echo "</td>";
                echo "<td>";
                echo "<form action='' method='post'><input type='submit' value='Delete' name='".$v_delete."'/></form>";
                if(isset($_POST[$v_delete])) {
                  **$curr_dir = './'.$dir . '/images';**
                  $files = scandir($curr_dir);
                  foreach ($files as $file) {
                    if ($file != '.' && $file != '..') {
                        unlink($curr_dir.'/'.$file);
                    }
                  }
                  echo "delete done for directory: " . $curr_dir;
                }
                  echo "</td>";
                }
                $count++;
            }
        ?>
    


  • Hey @MPG-Radio

    I would add a hidden input field e.g.

    "<input type='hidden' name='file_path' value='".$str_URL"'>"
    

    I am not sure if the above in syntactically correct but you should get the idea. The type should be 'hidden' and the value should be the path to the file.

    Once you figure out the above method of inserting the file name / path into a hidden field. You should be able to use PHP unlink() method to delete and file and rmdir() to remove a directory.

    So something like

    unlink($_POST['file_path']);
    

    and

    rmdir('./');
    

    Again, I have not tested the above examples. Just spitting out some ways it could possibly work. It's up to you to make it work.

    I hope this was of some help.



  • With the original code below, if try to use a hidden input type I lose my delete button, not sure how to get that back.
    I have set up a working test page if you would like to see it and the file structure: http://monsterworld.ocry.com/test/
    Of course, I worry about that being safe towards injections...

    <html>
    <head>
        <title>Delete Selected file</title>
    </head>
    <body>
        <iframe id="my_iframe" style="display:none;"></iframe>
        <table border="1">
        <?php
            $dir = './';
            $files = scandir($dir);
            sort($files);
            $count = -1 ;
            foreach ($files as $file) {
                $v_delete = "delete_".$count;
                    if ($file != '.' && $file != '..' && $file != 'read.php') {
                    $str_URL = "./".$file;
                    echo "<tr>";
                    echo "<td>";
                    echo $count;
                    echo "</td>";
                    echo "<td>";
                    echo $file;
                    echo "</td>";            
                    echo "<td>";
                    echo "<form action='' method='post'><input type='submit' value='Delete' name='".$v_delete."'/></form>";
                    if(isset($_POST[$v_delete])) {
                    // Your php delete code here
                       unlink($dir.'/'.$file);
                    echo "delete file : ".$file;
                    }
                    echo "</td>";
                   
                }
                $count++;
            }
        ?>
    </table>
    </body>
    </html>


  • @MPG-Radio Just to confirm. You want to delete the file AND the folder that it is contained in?



  • Yes sir.



  • @MPG-Radio I got it.

    I misunderstood your question at first. To delete the content of the selected folder you could add the following code snippet into the body of the if statement.

    if(isset($_POST[$v_delete])) {
                  $curr_dir = './'.$dir;
                  $files = scandir($curr_dir);
                  foreach ($files as $file) {
                    if ($file != '.' && $file != '..') {
                        unlink($curr_dir.'/'.$file);
                    }
                  }
                  echo "delete done for directory: " . $curr_dir;
                }
    

    Just fyi I have changed some of the names.

    Here is the complete code that worked for me:

    <html>
    <head>
        <title>Delete Selected file</title>
    </head>
    <body>
        <iframe id="my_iframe" style="display:none;"></iframe>
        <table border="1">
        <?php
            $root_dir = './';
            $directories = scandir($root_dir);
            sort($directories);
            $count = -1 ;
            foreach ($directories as $dir) {
                $v_delete = "delete_".$count;
                if ($dir != '.' && $dir != '..' && $dir != 'index.php') {
                $str_URL = "./".$dir;
                echo "<tr>";
                echo "<td>";
                echo $count;
                echo "</td>";
                echo "<td>";
                echo $dir;
                echo "</td>";
                echo "<td>";
                echo "<form action='' method='post'><input type='submit' value='Delete' name='".$v_delete."'/></form>";
                if(isset($_POST[$v_delete])) {
                  $curr_dir = './'.$dir;
                  $files = scandir($curr_dir);
                  foreach ($files as $file) {
                    if ($file != '.' && $file != '..') {
                        unlink($curr_dir.'/'.$file);
                    }
                  }
                  echo "delete done for directory: " . $curr_dir;
                }
                  echo "</td>";
                }
                $count++;
            }
        ?>
    </table>
    </body>
    </html>
    

    Let me know how that worked for you.



  • Thank you, so that means with one more revision we can say delete all the files in a subfolder in those viewable directories called images.

    <?php
            $root_dir = './';
            $directories = scandir($root_dir);
            sort($directories);
            $count = -1 ;
            foreach ($directories as $dir) {
                $v_delete = "delete_".$count;
                if ($dir != '.' && $dir != '..' && $dir != 'index.php') {
                $str_URL = "./".$dir;
                echo "<tr>";
                echo "<td>";
                echo $count;
                echo "</td>";
                echo "<td>";
                echo $dir;
                echo "</td>";
                echo "<td>";
                echo "<form action='' method='post'><input type='submit' value='Delete' name='".$v_delete."'/></form>";
                if(isset($_POST[$v_delete])) {
                  **$curr_dir = './'.$dir . '/images';**
                  $files = scandir($curr_dir);
                  foreach ($files as $file) {
                    if ($file != '.' && $file != '..') {
                        unlink($curr_dir.'/'.$file);
                    }
                  }
                  echo "delete done for directory: " . $curr_dir;
                }
                  echo "</td>";
                }
                $count++;
            }
        ?>
    


  • @MPG-Radio

    Are you asking if that is what I did? If so yes. The first loop is to iterate over all folders in the current directory and the second loop is to iterate over all files in each directory. You might want to add a check to make sure that the first loop iterates only over directories and the second loop only iterates over files.

    Let me know if I misunderstood.



  • It's perfect, I did make that small change to delete from the images folder. Without your input, I likely wouldn't have figured that out. This is the small change made.

    $curr_dir = './'.$dir . '/images';

    So with more work, I have styled things up to be like this.
    delete.jpg

    I will add that the purpose of all this was to manage the unneeded images that are placed in a directory via FTP from security cams. I hope I'm not rambling too much.


Log in to reply