1
我是新来的PHP,并试图通过代码来实现以下功能给无法加载PDF文档中我的PHP代码
功能: 我有一个下载按钮,在点击我应该能够下载我的.pdf文件存储在htdocs下 - > xxfilename - > abc.pdf;
代码: 我使用下面的代码在我的xyz.php网页
<?php
$title = "Learning";
$content = '
<h3> Intro</h3>
<p>
Introduction goes here
</p>
<form action = "xyz.php" method = "post" name = "downloadform">
<input type="submit" value="Download" name="dwnld_file"/>
</form>
';
if (isset($_POST['dwnld_file'])) {
mysql_connect("localhost", "root", "");
mysql_select_db("PQR");
$res = mysql_query("Select * from tab1");
while ($row = mysql_fetch_array($res)) {
$file = '$row["col2"]';
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="' . $row["col2"] . '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
header('Content-Length:' . filesize($file));
readfile($file);
}
}
include 'Template.php';
?>
错误: 我的PDF文件是越来越下载,但在打开它,它说“无法加载PDF文档”
请帮我在哪里错了。
***编辑**** 我尝试不同的方法,我的文件被下载但仍说“无法加载PDF文档”
我的另一种方法的代码如下
<form action = "xyz.php" method = "post" name = "downloadform">
<input type="submit" value="Download " name="dwnld_file"/>
</form>
<?php
if (isset($_POST['dwnld_file'])) {
mysql_connect("localhost", "root", "");
mysql_select_db("test");
$res = mysql_query("Select * from tab1");
while ($row = mysql_fetch_array($res)) {
$file = $row["col1"];
echo $file;
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="' .$file. '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
header('Content-Length:' . filesize($file));
readfile('myfile/'.$file);
}
}
?>
请告诉我,如果我做错了什么。
这是非常丑陋的执行方式。 – Amit
看起来像是有一个类似的问题在这里接受的答案http://stackoverflow.com/questions/1004478/read-pdf-files-with-php –
@Amit:你可以建议任何更好的方式吗? 请注意,我有我的所有网页的通用模板。并在页面中的一个我需要显示下载PDF按钮下载PDF文件。 –