如何从数据库下载文件

时间:2015-02-23 21:05:24

标签: javascript php html mysql download

我想使用属性sub_name从我的上传表中下载文件, 需要帮忙。我有两个PHP文件的代码,其中一个显示其他文件的下载,但它不起作用。

uplaods表包括sub_name,upload_name,file_content(实际上是上传的文件)。

我的代码(发送数据的表格是下载的):

<?php
    include_once("functions.php");
    $query="SELECT * from uploads";
    $rs = execute_query($query);
?>

<form  action="s_downfinl.php" method="post" enctype="multipart/form-data">
<table border="groove" cellpadding="15px">
<tr>
<td>Subject Name</td>
<td>upload name</td>
<td>Download</td>
</tr>
<?php
    while ($row = mysql_fetch_assoc($rs)){
        echo "<tr><td>";

        echo $row['sub_name']."</td><td>";  
        echo $row['upload_name']."</td>";
        echo "<td><a href='s_downfinl.php?nama=".$file."'>download</a></td>";
    }

    echo "</table>";
?>
</form>

downlaod.php

    <?php
include_once"functions.php()";

$query = "SELECT * FROM uploads WHERE sub_name = $sub_name";
$rs = execute_query($query);
$name= $_GET['nama'];
download($name);

function download($name){
$file = $nama_fail;

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

fuctions.php(用于数据库连接)

<?php
function execute_query($query){
$connection = mysql_connect("localhost","root","");
mysql_select_db("ascs",$connection);
$result_set = mysql_query($query);
mysql_close($connection);
return $result_set;
}
?>

0 个答案:

没有答案