【问题标题】:PHP MySqli Can't download file from the database. Failed to load PDF documentPHP MySqli 无法从数据库下载文件。无法加载 PDF 文档
【发布时间】:2019-05-21 16:13:02
【问题描述】:

我正在尝试从我使用路径上传的数据库中下载一个文件并将该文件复制到一个文件夹中。当我下载文件并尝试打开它时,它显示“加载 PDF 文档失败”。我不知道我做错了什么。有人能帮助我吗?谢谢大家。

这是我的上传文件代码:

$contract_file = basename($_FILES['contractupload']['name']);
$contract_path = "files/contracts/$contract_file";
$contract_file = mysqli_real_escape_string($conn, $contract_file);

if (copy($_FILES['contractupload']['tmp_name'], $contract_path)){

$sql = "INSERT INTO addemployees (contractupload)

        VALUES ('$contract_file')";

这是我的下载代码:

<?php

// Include config file
require_once "config.php";

if(isset($_GET['id'])) { // if id is set then get the file with the id from database

    $id = $_GET['id'];

    $query = "SELECT contractupload FROM addemployees WHERE id = $id";

    $result = mysqli_query($mysqli, $query) or die('Error, query failed');

    list($contractupload) = mysqli_fetch_array($result);

    header("Content-Type: application/octet-stream");
    header("Content-Disposition: attachment; filename=" . Urlencode($contractupload));
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");
    header("Content-Description: File Transfer");

    echo $contractupload; exit;
}
?>

Download File From MySQL

<?php

$query = "SELECT id, contractupload FROM addemployees";

$result = mysqli_query($query) or die('Error, query failed');

if(mysqli_num_rows($result) == 0)
{
    echo "Database is empty";
}
else
{
    while(list($id, $contractupload) = mysqli_fetch_array($result))
    {
?>

<?php
    }
}
?>

我将它列出/展示给我的桌子:

<?php
$conn = mysqli_connect("localhost", "root", "", "employees");

if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT * from addemployees";
$result = $conn-> query($sql);

if ($result-> num_rows > 0) {
    while ($row = $result-> fetch_assoc()) {

        echo "<tr>
                 <td>".$row['id']."</td>
                 <td>".$row['fname']."</td>
                 <td>".$row['lname']."</td>
                 <td>".$row['dob']."</td>
                 <td>".$row['embg']."</td>
                 <td>".$row['workposition']."</td>
                 <td>".$row['address']."</td>
                 <td><a href='download2.php?id=". $row['id'] ."' title='Download File'><span style='font-size: 19px; color: #3277b6; margin-right: 15px;'><i class='far fa-eye'></i></span></a></td>

                 <td>
                     <a href='read.php?id=". $row['id'] ."' title='View'><span style='font-size: 19px; color: #3277b6; margin-right: 15px;'><i class='far fa-eye'></i></span></a>
                     <a href='update.php?id=". $row['id'] ."' title='Edit'><span style='font-size: 19px; color: #5cb85c; margin-right: 15px;'><i class='fas fa-pencil-alt'></i></span></a>
                     <a href='delete.php?id=". $row['id'] ."' title='Delete'><span style='font-size: 19px; color: red;'><i class='fas fa-trash-alt'></i></span></a>
                     </td>
                 </tr>";
    }
    echo "</table>";
} else {
    echo "0 results";
}

$conn-> close();
?>

【问题讨论】:

    标签: php mysql mysqli


    【解决方案1】:

    $contactupload 只是文件名。您需要返回文件内容,而不是文件名。

    改变

    echo $contractupload;
    

    readfile("files/contracts/$contractupload");
    

    另外,您不应该有多个 Content-type: 标头。如果这应该是 PDF,它应该是:

    header("Content-type: application/pdf");
    

    【讨论】:

    • 谢谢@Barmar 我已经改变了,但我仍然得到Error
    • 你确定文件是PDF吗?
    • 是的,我上传到数据库的所有文件都是PDF。如果我尝试从它们存储的文件夹中“手动”打开它们,它们工作正常(它们正在打开)。
    • 这样更有帮助。检查服务器上的 PHP 错误日志,看看它是否在 readfile() 中出现错误。
    • 应该是filesize("files/contracts/$contractupload")
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多