从mysql数据库读取pdf文件 [英] reading a pdf file from a mysql database

查看:126
本文介绍了从mysql数据库读取pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此示例 http://www.php-mysql-tutorial.com/wikis/mysql-tutorials/uploading-files-to-mysql-database.aspx

上传部分正在顺利进行.但是我在下载部分面临很多问题.我想创建一个链接,用户单击该链接,然后他便可以下载适当的pdf.但是该页面运行,并且锚标记可见.但是,当我单击它时,.htm文件正在下载 下面是我的download.php

The upload part is happening without any trouble. But I am facing a lot of problems in the download part of it. I want to create a link on which the user clicks and then he is able to download the appropriate pdf. But the page runs and the anchor tag is visible. But when i click on it .htm file is getting downloaded below is my download.php

<html>
<head>
<title>Download File From MySQL</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
include 'config.php';
include 'opendb.php';

$query = "SELECT id, name FROM upload";
$result = mysql_query($query) or die('Error, query failed');
if(mysql_num_rows($result) == 0)
{
echo "Database is empty <br>";
}
else
{
while(list($id, $name) = mysql_fetch_array($result))
{
?>
<?php echo "<a href='download.php?id='".$id.";>Download</a> <br>";

}
}
include 'closedb.php';
?>
</body>
</html>
<?php
if(isset($_GET['id']))
{
// if id is set then get the file with the id from database

include 'config.php';
include 'opendb.php';

$id    = $_GET['id'];
$query = "SELECT name, type, size, content " .
         "FROM upload WHERE id = '$id'";

$result = mysql_query($query) or die('Error, query failed');
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;

include 'closedb.php';
exit;
}

?>

推荐答案

问题是,无论用户是要查看要下载的文件还是要下载文件,都在页面顶部启动html输出.文件本身.在那一刻,要下载的文件变成一个html页面.

The problem is that at the top of the page you start the html output regardless whether the user wants to see the files to be downloaded, or wants to download the file itself. In that moment the file to be downloaded bacomes an html page.

您的代码应首先检查是否设置了id参数.如果不是,则开始生成html页面.如果是,则从数据库中读取文件,设置标题并将内容发送回去.

Your code should start with a check if the id parameter is set. If not, then you start generating the html page. If yes, then you read the file from the db, set the headers and send the contents back.

或者只是使用另一个php页面来显示文件列表并下载其中一个文件.

Or just use a different php page for displaying the list of files and downloading one of the files.

这篇关于从mysql数据库读取pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆