从MySQL数据库引入文件名时,PHP文件下载链接不起作用
当从MySQL数据库通过PHP创建下载链接时,我很难让它们正常工作。用于获取文件名的代码如下(GET值用于测试;该值通常来自另一个网页): 接下来是基于从MySQL数据库引入文件名时,PHP文件下载链接不起作用,php,mysql,download,Php,Mysql,Download,当从MySQL数据库通过PHP创建下载链接时,我很难让它们正常工作。用于获取文件名的代码如下(GET值用于测试;该值通常来自另一个网页): 接下来是基于attachid从MySQL数据库生成文件名表的代码: <?php $sql = "SELECT UID, attachId, name FROM requisitions.upload WHERE attachId = '$_GET[attachid]'"; $res6 = mysql_query($sql) or die(mysql_
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>
$\u 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; }
?>
请记住,您的代码目前完全容易受到SQL注入攻击,mysql_*已被弃用。强烈建议使用PDO/准备好的语句。在数据库中使用变量之前,至少要先转义$\u获取变量
编辑:在将变量名作为字符串的一部分添加之前,还需要将变量连接到标题中。请参阅上面的代码块。我看到两个问题。第一个脚本中的$\u GET
值周围缺少引号,第二个脚本中的$\u GET[attachid]
直接位于SQL字符串中,而不是使用{$\u GET['attachid']}添加变量
在URL中设置一个名为uid
的变量。但是您正在检查一个名为UID
,它区分大小写。
$_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; }
?>