无法从数据库下载pdf文件,显示普通记事本输出


Unable to download a pdf file from database, it show plain notepad out

我试图做一个ahref,能够从数据库下载pdf文件,但我点击它后下载一个普通的记事本,而不是pdf和没有数据里面。

<a href="download.php?id=<?php=$id;?>">Download [pfd]</a> <br>

这是download.php

<?php
// Connect to the database
        $host="localhost"; // Host name 
        $username="root"; // Mysql username 
        $password=""; // Mysql password 
        $db_name="is"; // Database name 
        $tbl_name="publication"; // Table name 
        $conn = mysql_connect("$host", "$username", "$password"); 
        if(! $conn )
        {
          die('Could not connect: ' . mysql_error());
        }
        mysql_select_db($db_name);
    if (isset($_GET["id"]) && !empty($_GET["id"])) { 
            $id= $_GET['id'];
            $query = "SELECT file_name, file_type, file_size, content " .
                     "FROM $tbl_name WHERE file_id = '$id'";
            $result = mysql_query($query) or die(mysql_error()); 
            list($name, $type, $size, $content) = mysql_fetch_array($result);
            header("Content-length: $size");
            header("Content-type: $type");
            header("Content-Disposition: attachment; filename=$name");
            echo $content;
            mysql_close($conn);
            exit;
            }

?>

我有存储文件名,类型,大小和数据库内的内容,但无法下载它们为pdf。文件类型为application/pdf

我猜你的结果集是空的:你的查询不匹配任何行。这解释了空文件内容&空文件名

由于即使存在错误,此页面也强制下载文件,因此您可能会错过错误消息。您可以尝试暂时删除下载标头以查看错误。也可以在点击链接的同时按下CTRL键,在新窗中打开下载链接。通过这种方式,您可以验证下载URL确实包含ID。

BTW:你的代码中有SQL注入漏洞