title: PHP list directory and delete or download files
body: <p>I have a directory on my server, I want to list all the files in it with checkboxes and either delete or download them after choosing.</p>
  
  <p>So far I can list and delete fine using this form, ticking checkboxes and pressing the button. But I also sometimes need to download files to my local machine. Either by ticking the checkbox or right-clicking the file name and downloading with 'save file as' in Chrome browser. But I can't get it working.</p>
  
  <p>How can I download these files?</p>
  
  <p>My page is called download-ui.php</p>
  
  <pre><code>&lt;?php
  
  if(isset($_POST['Submit']))
      {
      }      
        foreach ($_POST['select'] as $file) {
  
      if(file_exists($file)) {
          unlink($file); 
      }
      elseif(is_dir($file)) {
          rmdir($file);
      }
  }
  
  $files = array();
  $dir = opendir('.');
      while(false != ($file = readdir($dir))) {
          if(($file != ".") and ($file != "..") and ($file != "download-ui.php") and ($file != "error_log")) {
                  $files[] = $file; 
          }   
      }
  
      natcasesort($files);
  ?&gt;
  
  &lt;form id="delete" action="" method="POST"&gt;
  
  &lt;?php
  echo '&lt;table&gt;&lt;tr&gt;'; 
  for($i=0; $i&lt;count($files); $i++) { 
      if ($i%5 == 0) { 
          echo '&lt;/tr&gt;';
          echo '&lt;tr&gt;'; 
      }       
      echo '&lt;td style="width:180px"&gt;
              &lt;div class="select-all-col"&gt;&lt;input name="select[]" type="checkbox" class="select" value="'.$files[$i].'"/&gt;
              &lt;a href="download-ui.php?name='.$foldername."/".$files[$i].'" style="cursor: pointer;"&gt;'.$files[$i].'&lt;/a&gt;&lt;/div&gt;
              &lt;br /&gt;
          &lt;/td&gt;';
  }
  echo '&lt;/table&gt;';
  ?&gt;
  &lt;/table&gt;
  &lt;br&gt;
  &lt;button type="submit" form="delete" value="Submit"&gt;Delete File/s&lt;/button&gt;
  &lt;/form&gt;&lt;br&gt;
  </code></pre>