Can't download file from the database. Failed to load PDF document

I’m trying to download a file from my database which i have uploaded using path and copy the file to a folder. When i download the file and try to open it it says “Failed to load PDF document”. I don’t know what i’m doing wrong. Can someone help me please? Thanks guys.

This is my upload file code:

    $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')";

And this is my download code

    <?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

    }

    }

    ?>

I’m listing it to my table:

    <?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();

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

This is likely your issue. You are storing an array there, not the file name.

Sponsor our Newsletter | Privacy Policy | Terms of Service