我正在开发一个网站,我想从服务器上传和下载数据。上传工作正常,但我无法下载数据。我有一个名为 upload
的表,其中包含以下列:uplaod_name
、sub_name
、semester_id
、date
和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 "<pre>";
//print_r($row);
//echo "</pre>";
echo $row['sub_name']."</td><td>";
echo $row['upload_name']."</td>";
echo "<td><a href='s_downfinl.php?nama=". $row['file_content']."'>download</a></td>";
}
echo "</table>";
?>
</form>
以及用于下载文件的 s_downfinl.php 文件:
<?php
include_once("functions.php");
$query = "SELECT * FROM uploads WHERE sub_name = $sub_name";
$name= $_POST['nama'];
download($name);
$rs = execute_query($query);
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;
}
}
?>
最佳答案
首先,where
子句中的 $sub_name
变量 WHERE sub_name = $sub_name
- 如果它是一个字符串(which我的印象是),需要引用WHERE sub_name = '$sub_name'
。使用 or die(mysql_error())
到 mysql_query()
会/应该触发语法错误。
目前,尚不确定该变量是在何处以及如何定义的。
加上这个 $_POST['nama']
,它应该是一个 GET,按照 s_downfinl.php?nama
。
测试时使用错误报告检查错误。
添加error reporting到文件顶部,这将有助于查找错误。
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
旁注:错误报告只能在暂存阶段完成,而不能在生产阶段完成。
也可以使用mysql_error
对于您的查询,查看您的查询是否失败。
脚注:
$nama_fail
根据您发布的代码未定义,因此您可能需要详细说明,$sub_name
也是如此。
查看这些有关从数据库下载文件的示例。
Download files from MYSQL database 该问题的答案之一https://stackoverflow.com/a/15560485/看起来是一个可行的例子。根据该示例,在没有附加函数的情况下尝试一下
function download($name){ $file = $nama_fail; ...}
.- http://www.wellho.net/resources/ex.php4?item=h110/pdfget.php
他们可能会给你想法/帮助。
关于php - 如何从 MySQL 数据库下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30012968/