0

I do a scandir of a specific dir and I display the file names in a table. Now I want to make every file downloadable but I am not sure how to do this.

here is where I print the filenames in a table:

<tbody>
            <?php
                $i = 0;
                foreach ($files as $file) {
                    $i++;
                    echo "<tr><th scope='row'>$i</th><td>$file</td><td><i class=\"fa fa-download\" aria-hidden=\"true\"></i></td></tr>";
                }
            ?>
</tbody>

I was thinking of using a function or php file with something like this

header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=file.zip");
header("Pragma: no-cache");
header("Expires: 0");
readfile("file.zip");

but I am not sure how to inject the filename and content type in my function of php file

Any help is greatly appreciated!

Many thanks in advance!

Frank Lucas
  • 551
  • 3
  • 11
  • 25
  • 2
    Possible duplicate of [php, file download](http://stackoverflow.com/questions/5595485/php-file-download) – Bobot Dec 26 '16 at 13:12
  • @Bobot No it is not duplicate I wanna be able to download a specific file based on the table i create from my array – Frank Lucas Dec 26 '16 at 13:15
  • `/download.php?file=` for the link, then see the possible duplicate – Bobot Dec 26 '16 at 13:17
  • @Bobot I am sorry but I don't understand your answer – Frank Lucas Dec 26 '16 at 14:21
  • you just have to do a link in your table like : `Download myfile.ext` for the list, and in `download.php` you just have to put the code from the "possible duplicate" answer. And while you want to protect a bit your system, you will want to encrypt the `/path/to/myfile.ext` and then decrypt it in the `download.php` because you will need it in clear text. there you can see [this answer which is clear enough](http://stackoverflow.com/a/30166085/3799829) – Bobot Dec 26 '16 at 14:50

0 Answers0