How to make a download button for files taken from a database?

后端 未结 2 750
梦如初夏
梦如初夏 2021-01-23 08:57

I need to print out all the files that I\'ve saved in a database and then make it possible to download them. I am able to echo them, but I don\'t know how to make a download \"b

2条回答
  •  半阙折子戏
    2021-01-23 09:34

    I once made a similar script, what you have to do is set some headers and echo the data from database.

    An example of that could be, assuming that the data is in $row['filedata'], the following

    
    

    Now you have to know what file to download first, and for that we can use a GET or POST parameter

     1 || $result_check < 1){ //If more then 1 result die
        die('inavlid ID');
    }
    $row = mysqli_fetch_assoc($result);
    //and here the above mentioned lines
    ?>
    

    The easiest way to add a download button to the page would be by using the window.open() function in JavaScript like so:

    echo $row['Ime']."
    ";

    This will open a new window which will download the file

    The total would look something like this For download.php:

     1 || $result_check < 1){ //If more then 1 result die
        die('inavlid ID');
    }
    $row = mysqli_fetch_assoc($result);
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-Disposition: attachment; filename=".$row['Ime']);
    header("Content-Type: application/zip");
    header("Content-Transfer-Encoding: binary");
    echo $row['filedata'];
    ?>
    

    and for index.php

     0){
        while($row = mysqli_fetch_assoc($result)){
            echo $row['Ime']."
    "; } } ?>

提交回复
热议问题