0

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

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 / showing 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();
?>
YakovL
  • 7,557
  • 12
  • 62
  • 102
Blagojce
  • 27
  • 1
  • 9

1 Answers1

1

$contactupload is just the name of the file. You need to return the file contents, not the filename.

Change

echo $contractupload;

to

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

Also, you shouldn't have multiple Content-type: headers. If this is supposed to be PDF, it should be:

header("Content-type: application/pdf");
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks @Barmar i have changed that, but i'm still getting the [Error](https://i.imgur.com/DLUCquM.png) – Blagojce Dec 21 '18 at 07:21
  • Are you sure the file is PDF? – Barmar Dec 21 '18 at 07:24
  • Yes, all my files uploaded in the db are PDF. And if i try to open them "manually" from the folder where they are stored, they are working fine (they are opening). – Blagojce Dec 21 '18 at 07:25
  • Check the file in a hex editor and make sure it looks like a correct PDF file. – Barmar Dec 21 '18 at 07:26
  • I just added/uploaded a new file to the db, and when i download it, it shows the same error :( – Blagojce Dec 21 '18 at 07:29
  • I just noticed that some of the downloaded files are with 0 bytes, and some of them are with it's original file size :( – Blagojce Dec 21 '18 at 07:31
  • 1
    That's more helpful. Check your PHP error log on the server to see if it's getting an error in `readfile()`. – Barmar Dec 21 '18 at 07:35
  • This is what i have in the php log file: [21-Dec-2018 07:38:34 UTC] PHP Stack trace: [21-Dec-2018 07:38:34 UTC] PHP 1. {main}() C:\wamp64\www\management3\download2.php:0 – Blagojce Dec 21 '18 at 07:39
  • There must be more lines in the trace. Also an error message before that. – Barmar Dec 21 '18 at 07:54
  • Sorry i didn't copy it properly: `[21-Dec-2018 07:55:10 UTC] PHP Notice: Undefined variable: file in C:\wamp64\www\management3\download2.php on line 20 [21-Dec-2018 07:55:10 UTC] PHP Stack trace: [21-Dec-2018 07:55:10 UTC] PHP 1. {main}() C:\wamp64\www\management3\download2.php:0` – Blagojce Dec 21 '18 at 07:57
  • There's no `$file` variable in the code. What does line 20 look like? – Barmar Dec 21 '18 at 07:59
  • `header("Content-Length:" .filesize($file));` – Blagojce Dec 21 '18 at 08:00
  • 1
    It should be `filesize("files/contracts/$contractupload")` – Barmar Dec 21 '18 at 08:01
  • Now i'm embarrassed :-/ Sorry about it @Barmar. When you point to it ( line 20 of the code ), i just realized that, that is what makes the problem. I removed that line and it works now. Thank you very much!!! – Blagojce Dec 21 '18 at 08:03
  • Barmar sorry to bother, i was wondering if you have time to check my issue here and perhaps help me please? [Update PDF file](https://stackoverflow.com/questions/53899369/php-mysqli-how-to-update-pdf-file-that-already-has-been-added-to-db) – Blagojce Dec 23 '18 at 18:28
  • I'm stuck with updating the file i uploaded. I need to be able to update the file ( add new file that will overwrite the old one ) using my update.php. I hope you can help me please. Thanks @Barmar – Blagojce Dec 23 '18 at 19:20