Delete multiple files at once

Discussion in 'PHP' started by web_warrior, Aug 31, 2009.

  1. #1
    I have this script which shows the content of a folder, each folder being shown like a link, which if clicked deletes that file.

    The problem is that it needs to be done separately with each file, and then go back from the result page, select another one file to delete and so on.

    How can be the script changed to make possible multiple file deletion with a single click?
    Something like having output a check box besides each file, and a single "delete" button, which if clicked deletes all the files with checked boxes?

    
    
    <html>
    
    <?php
    
    $action = (isset($_GET['action'])) ? $_GET['action'] : "";
    $dl = "upload/"; 
    $absolute_path = " ....path......"; 
    $websiteurl = "....url........ ";
    $websitename = "MySite";
    
    switch($action) {
    
    case "delete":
    $delfile = $_GET['delfile'];
    unlink ($dl . "/" . $delfile);
    
    echo 
    
    "<html>
    
    <head>
    <title>Delete Files</title>
    </head>
    
    <body>
    <B>File $delfile has been succesfully deleted.</B><BR><BR>
     <a href='delete.php'>Back to 'delete'</a>";
    
    break;
    
    default:
    
    echo 
    
    "<html>
    <head>
    <title>Delete Files</title>
    </head>
    <body> 
    
    <a href=$websiteurl>Return</a>";
    
    $list = "<table width=600 border=1 bordercolor=#000000 style=\"border-collapse: collapse\">";
    
    $list .= "<tr><td width=600><center>
    <b>Click on the file to delete it</b></center></td></tr>";
    
    $dir = opendir($absolute_path);
    
    while($file = readdir($dir)) {
    if (($file != "..") and ($file != ".")) {
    $list .= "<tr><td width=600>==> 
    <a href='" . $_SERVER['PHP_SELF'] . "?action=delete&delfile=$file'>$file</a></center></td></tr>";
    }
    }
    $list .= "</table>";
    echo $list;
    echo "<br><br>
    
    </body>
    </html>";
    
    break;
    
    }
    ?>
    
    </html>
    
    
    Code (markup):
     
    Last edited: Aug 31, 2009
    web_warrior, Aug 31, 2009 IP
  2. prasanthmj

    prasanthmj Member

    Messages:
    62
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    45
    #2
    You can create a check box group . One check box for each file.
    Sample code:
    
    
    <input type="checkbox" name="selectedFiles[]"
    value="file1" />file1<br />
      <input type="checkbox" name="selectedFiles[]"
    value="file1" />file2<br />
      <input type="checkbox" name="selectedFiles[]"
    value="file3" />file3<br />
    
    HTML:
    server side:
    
    $files = $_POST['selectedFiles'];
    
        $N = count($files);
    
       
        for($i=0; $i < $N; $i++)
        {
           echo "\n deleting file: ".$files[$i];
    
          //unlink($files[$i]);
        }
    
    PHP:
    The following page has a tutorial on handling check boxes:
    Handling checkbox in a PHP form processor
     
    Last edited: Sep 1, 2009
    prasanthmj, Sep 1, 2009 IP