Jump to content
Bale

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

Recommended Posts

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

                                  ?>

 

Share this post


Link to post
Share on other sites

Look at these two lines of code:

header("Content-Disposition: attachment; filename=" . Urlencode($contractupload));
echo $contractupload;

Now describe what $contractupload is.

Share this post


Link to post
Share on other sites

That is the name of my column (contractupload) in my database where i store the path of the file

Edited by Bale

Share this post


Link to post
Share on other sites

Your column may very well be named "contractupload" but I'm talking about the variable.

Describe what the variable $contractupload is. According to how you're using it on those two lines of code.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.