Php list files in directory with option to delete -


i have download-ui.php has code excludes , lists other files in directory, sorts them , gives each checkbox.

<?php     $files = array();     $dir = opendir('.');     while(false != ($file = readdir($dir))) {         if(($file != ".") , ($file != "..") , ($file != "download-ui.php") , ($file != "error_log") , ($file != "favicon.ico")) {                 $files[] = $file;          }        }      natcasesort($files);      foreach ($files $file) {         echo '<li class="browse-file">             <div class="select-all-col"><input name="select[]" type="checkbox" class="select" value="'.$file.'"/></div>             <div class="file-name-col"><a href="download-ui.php?name='.$foldername."/".$file.'" style="cursor: pointer;">'.$file.'</a></div>             <br />         </li>';     } ?> 

i'm trying work out how checkbox values these files if checked can deleted using unlink().

i know need form, need assist syntax , capturing values form, have far.

<form id="delete" action="delete.php" method="post">     <button type="submit" form="delete" value="submit">submit</button>     <input name="select[]" type="checkbox" class="select" value="'.$file.'"/> </form> 

then delete.php have

<?php?     foreach ($_post['select[]'] $file) {         if(file_exists($file)) {             unlink($file);          }         elseif(is_dir($file)) {             rmdir($file);         }     }     echo "files deleted successfully."; ?> 

i think problem input <input name="select[]" type="checkbox" class="select" value="'.$file.'"/>

the error i'm getting latest effort php warning: invalid argument supplied foreach() in delete.php

you want loop through each item of $_post['select'], foreach should say:

foreach ($_post['select'] $file) {  } 

notice post capitalized , select has no array brackets.


Comments