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文件。 –