【问题标题】:PHP File Download Links Not Working when filenames brought in from MySQL Database当从 MySQL 数据库中引入文件名时,PHP 文件下载链接不起作用
【发布时间】: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 和文件名(在本例中为三个)的表,每个文件名都显示为附加attachiduid 的链接,但是当我单击链接时,什么都没发生。我只是返回quotes.php,没有下载。知道我可能做错了什么吗?就像没有读取标题一样。我在 IIS 7 上运行它,如果这有影响的话。

【问题讨论】:

  • 我看到了两个问题。您在第一个脚本和第二个脚本中缺少 $_GET 值周围的引号,您的 SQL 字符串中直接有 $_GET[attachid],而不是使用 {$_GET['attachid']} 添加变量
  • 您在 URL 中设置了一个名为 uid 的变量。但是您正在检查一个名为 UID 的邮件 - 它区分大小写。

标签: php mysql download


【解决方案1】:

$_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/prepared 语句。在数据库中使用它们之前,至少要转义您的 $_GET 变量。

编辑:您还需要将变量连接到标题中,然后才将变量名称添加为字符串的一部分。请参阅上面的代码块。

【讨论】:

    猜你喜欢
    • 2014-08-10
    • 2015-03-01
    • 2013-10-07
    • 2013-03-20
    • 2018-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多