【发布时间】:2013-06-25 04:09:09
【问题描述】:
当下载链接是通过 PHP 从 MySQL 数据库创建时,我在获取下载链接时遇到了一些困难。用于获取文件名的代码如下(GET 值用于测试;该值通常来自另一个网页):
$_GET[attachid] = '2597'; //Test value for getting filenames
if(isset($_GET[attachid]) && isset($_GET[UID]))
{
$attachid = $_GET[attachid];
$uid = $_GET[UID];
$sql = "SELECT name, type, size, content FROM requisitions.upload WHERE attachId
='$attachid'";
$res = mysql_query($sql) or die(mysql_error());
list($name, $type, $size, $content) = mysql_fetch_array($res);
header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
echo $content;
exit; }
?>
接下来是根据attachid从MySQL数据库生成文件名表的代码:
<?php
$sql = "SELECT UID, attachId, name FROM requisitions.upload WHERE attachId =
'$_GET[attachid]'";
$res6 = mysql_query($sql) or die(mysql_error());
$name = 'name';
$attachid = 'attachId';
$uid = 'UID';
while($row = mysql_fetch_array($res6, MYSQL_ASSOC))
{
?>
<tr>
<td>
<?php echo $row['attachId']; ?>
</td>
<td>
<?php echo "<a href=quotes.php?attachid={$row['attachId']}&uid={$row['UID']}>
{$row['name']}</a></td>";
} </tr></table>
上面生成了一个带有正确attachid 和文件名(在本例中为三个)的表,每个文件名都显示为附加attachid 和uid 的链接,但是当我单击链接时,什么都没发生。我只是返回quotes.php,没有下载。知道我可能做错了什么吗?就像没有读取标题一样。我在 IIS 7 上运行它,如果这有影响的话。
【问题讨论】:
-
我看到了两个问题。您在第一个脚本和第二个脚本中缺少
$_GET值周围的引号,您的 SQL 字符串中直接有$_GET[attachid],而不是使用{$_GET['attachid']}添加变量 -
您在 URL 中设置了一个名为
uid的变量。但是您正在检查一个名为UID的邮件 - 它区分大小写。