php - 如何使用php下载pdf文件

标签 php mysql database pdf download

我想下载 pdf 文件或任何其他文件,但我的主要动机是下载 pdf 文件。当用户提交查询时,我们是否可以在浏览器上看到 pdf 文件,而不是下载。

这是我的上传代码,效果很好`

<!DOCTYPE html>
<html>
<body>

<form method="post" enctype="multipart/form-data">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr> 
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile"> 
</td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>

</body>
</html>

<?php
$uploadDir = 'C:\wamp\www\images\passport images';

if(isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$filePath = $uploadDir . $fileName;

$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db_dat";

$conn = mysqli_connect($servername, $username, $password, $dbname);


if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}

$query = "INSERT INTO upload2 (name, size, type, path ) VALUES ('$fileName', '$fileSize', '$fileType', '$filePath')";

mysqli_query($conn,$query) or die('Error, query failed : ' . mysql_error());



echo "<br>Files uploaded<br>";

}
?>

**现在下载代码出现问题**。实际上我希望用户在文本框中提交查询。该文本以及扫描的图像因此以 pdf 格式向他显示

<?php
if(isset($_GET['id']))
{
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db_dat";

$conn = mysqli_connect($servername, $username, $password, $dbname);

$id = $_GET['id'];
$query = "SELECT name, type, size, path FROM upload2 WHERE id = '1'";
$result = mysqli_query($conn,$query) or die('Error, query failed');
list($name, $type, $size, $filePath) = mysqli_fetch_array($result);

header("Content-Disposition: attachment; filename='".$name."'");
header("Content-length: $size");
header("Content-type: $type");

echo "<a href=" . $uploadDir .($row['userfile']) . ">
         {$row['userfile']}</a>";

exit;
}

 ?>

`

最佳答案

这是我的下载功能,您可以使用它来下载任何文件。

function dawnload_file($path) {
    if (file_exists($path) && is_file($path)) {
        // file exist
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename=' . basename($path));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($path));
        set_time_limit(0);
        @readfile($path);//"@" is an error control operator to suppress errors
    } else {
        // file doesn't exist
        die('Error: The file ' . basename($path) . ' does not exist!');
    }
}

这是我在浏览器中在线打开pdf文件的函数。

function read_pdf_online($path){
    if (file_exists($path) && is_file($path)){
        // file exist
        header("Pragma: public"); // required
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Cache-Control: private",false); // required for certain browsers
        header("Content-Type: application/pdf");
        header("Content-Transfer-Encoding: binary");
        header("Content-Length: ".filesize($path));
        @readfile( $path );

    else {
        // file doesn't exist
        die('File Not Found: ' . basename($path));
    }
}

关于php - 如何使用php下载pdf文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30437174/

相关文章:

mysql - GROUP BY 用于 SQL 中的连续行

PHP,限制用户输入错误值x次

mysql - MS Access SQL 更新表 A 和其他 2 个表的信息

mysql - 在除一列之外的所有列上选择不同的行

php - 将 MySQL 从 latin1 编码更改为 UTF-8

php - Mysql - 连接匹配项和非匹配项

PHP 创建日期范围

php - 如何在 Laravel 中对密码使用 MD5 哈希?