How do I write 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 to write in a delete function to delete the directory and the files in it and not sure how that is written can someone insert an answer.?
Thank you so much

<?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++;
    }
?>

Hi @bandfanforum and a warm welcome to the forum.

I would be tempted to experiment first by possibly renaming (ucfirst(…), strtoupper(…), etc) files and sub directories first because once deleted they are gone!

Check out the free online php manual and take note of the comments.

https://www.php.net/manual/en/function.rmdir.php

Let us know if you create a solution and post your results here or if you need further help then post further questions.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.