Jump to content

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

                                  ?>

 

Look at these two lines of code:

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

Now describe what $contractupload is.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.