使用php表单从mysql下载文件时遇到问题

时间:2013-04-10 18:16:18

标签: php mysql download

您好我刚刚开发一个原型,允许用户通过动态下拉列表选择一些选项,然后将一个小文件上传到mysql。我刚刚完成了正常工作的搜索功能并正确返回结果。我最近几天遇到的问题是允许用户下载搜索结果中返回的一个文件。我把结果放到一个html表中并使用一个表单作为下载选项,这是我怀疑表单问题所在,因为当我搞乱文件下载时,但是当用户点击上一页的搜索时。 / p>

我的阅读让我走得这么远,我已经看过一些问题,但似乎没有人能做到这一点。我仍然是PHP的新手,所以请原谅我凌乱的结构。任何帮助将不胜感激。

以下是results_page.php的脚本

 if (isset ($resultts) and ($tests))

{



$sql = "SELECT p.logID, p.fileData, p.fileName, p.mimeType, p.dateCreated, t.testName,      r.resultType
    FROM result AS r 
    INNER JOIN product_logs AS p 
    ON r.resultID=p.resultID
    INNER JOIN test AS t
    ON t.testID=r.testID
    WHERE t.testName LIKE '%$tests%'
    AND r.resultType LIKE '%$resultts%'
    ORDER BY p.dateCreated DESC";

$result1= mysqli_query($sql) or die (mysqli_error());



while ($row = mysqli_fetch_array($result1))
{
$filename = $row['fileName'];
$filetype = $row['mimeType'];
$filetest = $row['testName'];
$fileresult= $row['resultType'];
$filedate=$row['dateCreated'];
$fileid=$row['logID'];
$filedata=$row['fileData'];

echo"<table>";
echo"<tr><th>File Name</th><th>File Type</th><th>Test</th><th>Result</th><th>Date</th>    <th>options</th></tr>";
echo"<tr valign=top>";
echo "</tr>";
echo"<td><'".$filename."'</td>";
echo "<td><'".$filetype."'</td>";
echo"<td><'".$filetest."'</td>";
echo"<td><'".$fileresult."'</td>";
echo"<td><'".$filedate."'</td>";
echo "<td><form action= confirm_download.php method= post</td>";
echo "<div>";
echo "<input type =hidden name=action value= '".$filename."' />";
echo "<input type=submit name=action value=download/>";
echo "<div>";
echo "</form>";
echo "</td>";

echo "</table>";
}





if ($_POST['action']=='download')
{
// Content-type must come before Content-disposition
header("Content-type: $filetype");
header("Content-disposition: attachment filename=$filename");
header('Content-length: ' . strlen($filedata));


echo $filedata;
exit();
}



}   

1 个答案:

答案 0 :(得分:0)

您有2个带名称操作的输入。从提交按钮中删除name = action。

echo "<input type =hidden name=action value= '".$filename."' />";
echo "<input type=submit name=action value=download/>";

所以让它像这样

echo "<input type =hidden name=action value= '".$filename."' />";
echo "<input type=submit value=download/>";

修改

好的,现在修复您的<form>标记。缺少> ...它应该在<td>之前打开或在该td内关闭。

并关闭<div>代码,因为现在您打开代码两次,而不是打开一次并关闭它一次。因此,第二个<div>应该成为</div>

所以这......

echo "<td><form action= confirm_download.php method= post</td>";
echo "<div>";
echo "<input type =hidden name=action value= '".$filename."' />";
echo "<input type=submit name=action value=download/>";
echo "<div>";

应该成为这个......

echo "<form action= confirm_download.php method= post>";
echo "<div>";
echo "<input type =hidden name=action value= '".$filename."' />";
echo "<input type=submit name=action value=download/>";
echo "</div>";
echo "</form>";

编辑2

好的,你现在的新问题是:

echo "<input type =hidden name=action value= '".$filename."' />";

这一行使$ _POST ['action']填充文件名。

但后来你说:

if ($_POST['action']=='download')

永远不会成真(除非文件名实际上是'下载'...) 您可以将if更改为:

if (isset($_POST['action']))