按下按钮从数据库下载php文件

时间:2016-11-15 19:17:01

标签: php download

我正在尝试使用按钮触发从数据库下载文件。

我使用以下代码将文件上传到我的数据库:

$loanform = $_FILES['loanform']['name'];
$uploadfile = "loanforms/$loanform";
move_uploaded_file($_FILES['loanform']['tmp_name'],$uploadfile);

数据库存储文件名,实际文件存储在网络服务器的 loanforms 目录中。

当我点击查看特定用户的记录时,我的数据库中显示的所有数据都会显示一个按钮来下载文件。但是,当点击按钮时没有任何反应。

要获取用户的信息,请使用以下代码;

$loanid=$_GET['loanid']; 

$my_query="select * from loans where loanid=$loanid";
$result= mysqli_query($connection, $my_query);

while ($myrow = mysqli_fetch_array($result)):

    $username = $myrow["username"];
    $emailaddress = $myrow["emailaddress"];
    $product = $myrow["product"];
    $product_desc = $myrow["product_desc"];
    $serial = $myrow["serial"];
    $date_collected=$myrow["date_collected"];
    $date_return=$myrow["date_return"];
    $loanform=$myrow["loanform"];
    $returned=$myrow["returned"];

endwhile;   

if (file_exists($loanform)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="'.basename($loanform).'"');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        readfile($loanform);
        exit;
    }

按钮的HTML是:

<label class="labelform">Loan Form:</label><input class="inputtextlarge" type="button" name="loanform" autocomplete="off" size="30" value="Download Loan Form">

基本上对于 loanid = 1 ,存储的文件名是 loanform.pdf 。如何调整按钮的代码以触发文件下载?

1 个答案:

答案 0 :(得分:0)

你的按钮什么都不做,因为它只是按钮。让它链接:

<label class="labelform">
    Loan Form:
</label>
<a href="getLaon.php?loanid=<?= $loanId; ?>" class="inputtextlarge">Download Loan Form</a>

另请注意您提出的有关安全问题的评论。