1

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.

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.

How can I download these files?

My page is called download-ui.php

<?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);
?>

<form id="delete" action="" method="POST">

<?php
echo '<table><tr>'; 
for($i=0; $i<count($files); $i++) { 
    if ($i%5 == 0) { 
        echo '</tr>';
        echo '<tr>'; 
    }       
    echo '<td style="width:180px">
            <div class="select-all-col"><input name="select[]" type="checkbox" class="select" value="'.$files[$i].'"/>
            <a href="download-ui.php?name='.$foldername."/".$files[$i].'" style="cursor: pointer;">'.$files[$i].'</a></div>
            <br />
        </td>';
}
echo '</table>';
?>
</table>
<br>
<button type="submit" form="delete" value="Submit">Delete File/s</button>
</form><br>
Kilisi
  • 402
  • 11
  • 33

1 Answers1

2

The download-ui.php have to be something like:

//Only enter if there is some file to download.
if(isset($_GET['name']){
  $file = basename($_GET['name']); //change if the url is absolute

  if(!$file){ // file does not exist
    die('file not found');
  } else {
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-Disposition: attachment; filename=$file");
    header("Content-Transfer-Encoding: binary");

    // read the file from disk
    readfile($file);
  }
}

//your form

This way you are forcing to download the file. If you want it better, you can make a AJAX call when click on 'download' pointing to that url, and the file will be downloaded async, like:

$.ajax({
    url: 'download-ui.php',
    type: 'POST',
    success: function() {
        window.location = 'download-ui.php';
    }
});

But without AJAX will work too.

Check this: Download files from server php

Hope it helps!

Community
  • 1
  • 1
JP. Aulet
  • 4,375
  • 4
  • 26
  • 39
  • That form is in download-ui.php, where would I put this code? Under the other? – Kilisi May 22 '17 at 10:57
  • If you use the same file for the form & for download the files, you should put a if in the beggining, wrapping all my code. Check my update! If you use the AJAX, take in consideration changing all the $_GET for $_POST. If you don't want the page to stop if something is wrong, delete the 'die'. – JP. Aulet May 22 '17 at 10:59
  • I can't use AJAX, still lost on how your example will work. How does it know which file to download? If I tick the box of one it will delete it. If I rightclick the filename it tries to save a link called download-ui.htm. Do I need a 'Download' button? – Kilisi May 22 '17 at 11:15
  • Without AJAX will work. You have to paste the php code in your first part of the file. The structure will be something like: - if(submit) { //delete } - if(get) { //download} loadFiles showFiles You have an error in the code `$foldername` (this var is undefined). You should use `$dir` or something like this. If you click on the link of the name of the file, this should make the download if is all right. – JP. Aulet May 22 '17 at 12:51
  • if I change foldername to dir is still won't download on right click, I think this is where the problem is, it's not giving me a proper link? It just tries to download download-ui.htm – Kilisi May 22 '17 at 13:15
  • I'm convinced my href is wrong? any idea how to fix it? – Kilisi May 22 '17 at 13:19
  • Which is the URL of the href? Why right click? You have to do a 'normal' click. The url should be like: ./filename.txt, of ./filename.pdf.. – JP. Aulet May 22 '17 at 13:24
  • I fixed it, I can now right click and save link as and get my file downloaded by browser... I changed to '.$files[$i].' thank you very much for the help, you put me on the right track. – Kilisi May 22 '17 at 13:28
  • Thats nice, your welcome! Upvote if the response helped ;) – JP. Aulet May 22 '17 at 13:29