如何为从数据库获取的文件制作下载按钮? [英] How to make a download button for files taken from a database?

查看:29
本文介绍了如何为从数据库获取的文件制作下载按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要打印出我保存在数据库中的所有文件,然后才能下载它们.我能够回应它们,但是我不知道如何进行下载按钮"以将它们下载到计算机. datbase有两列:ID和Ime.

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 "button" to download them to the computer. The datbase has two columns: ID and Ime.

我尝试使用HTML中的标记,但没有运气.我能够在文件名旁边回显按钮,但是我无法下载它们(我尝试使用file_put_contents方法)

I tried using a tag from HTML with no luck. I was able to echo buttons next to file names, but I couldn't download them (I tried using file_put_contents method)

这是输出文件名的代码(Ime =用我的语言命名).

This is the code that outputs file names (Ime = Name in my language).

<?php
$conn = mysqli_connect('localhost','root','usbw','down');
$query = "SELECT * FROM datoteka";
$result = mysqli_query($conn,$query);
$result_check = mysqli_num_rows($result);
if($result_check > 0){
    while($row = mysqli_fetch_assoc($result)){
        echo $row["Ime"]."<br>";
    }
}
?>

输出看起来像这样:

datoteka2019-04-01-15-44.doc
datoteka2019-04-01-15-48.doc
datoteka2019-04-01-15-16.pdf

我希望能够下载的数据库文件(目前为3个,可能更多)

Files in database that I want to be able to download (3 for now, could be more)

网站上每个文件名旁边应有一个下载按钮(或类似的东西),使我可以从数据库中下载该文件(一次仅下载一个文件,而不是一次下载所有文件).

There should be a download button (or something cliclkable) next to each file name on website that would enable me to download this file from the database (one file at time, not all at once).

推荐答案

我曾经做过一个类似的脚本,您要做的是设置一些标头并从数据库中回显数据.

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

例如,假设数据位于$row['filedata']中,则如下

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

<?php
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'];
?>

现在您必须知道首先要下载什么文件,为此,我们可以使用GET或POST参数

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

<?php
$fileID = htmlentities($_GET['fileID']);
$conn = mysqli_connect('localhost','root','usbw','down');
$fileID = mysqli_real_escape_string($conn, $fileID); // ALWAYS ESCAPE USER INPUT
$query = "SELECT * FROM datoteka where `ID`=$fileID";
$result = mysqli_query($conn, $query);
$result_check = mysqli_num_rows($result);
if($result_check > 1 || $result_check < 1){ //If more then 1 result die
    die('inavlid ID');
}
$row = mysqli_fetch_assoc($result);
//and here the above mentioned lines
?>

向页面添加下载按钮的最简单方法是使用JavaScript中的window.open()函数,如下所示:

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']."<button onclick='window.open(\"download.php?fileID=".$row['ID']."\");'>Download</button><br>";

这将打开一个新窗口,该窗口将下载文件

This will open a new window which will download the file

总数看起来像这样 对于download.php:

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

<?php
$fileID = htmlentities($_GET['fileID']);
$conn = mysqli_connect('localhost','root','usbw','down');
$fileID = mysqli_real_escape_string($conn, $fileID); // ALWAYS ESCAPE USER INPUT
$query = "SELECT * FROM datoteka where `ID`=$fileID";
$result = mysqli_query($conn, $query);
$result_check = mysqli_num_rows($result);
if($result_check > 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'];
?>

以及index.php

and for index.php

<?php
$conn = mysqli_connect('localhost','root','usbw','down');
$query = "SELECT * FROM datoteka";
$result = mysqli_query($conn,$query);
$result_check = mysqli_num_rows($result);
if($result_check > 0){
    while($row = mysqli_fetch_assoc($result)){
        echo $row['Ime']."<button onclick='window.open(\"download.php?fileID=".$row['ID']."\");'>Download</button><br>";
    }
}
?>

这篇关于如何为从数据库获取的文件制作下载按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆