-1

I have a MySQL database that shows various user inputs from a web form. On a separate page, the database is shown to my employees. I want to have a link in the database that shows the uploaded PDF file when it is clicked.

I have researched this issue for 2 weeks now, and none of the solutions have worked for me. I have searched Stack Overflow, as well as multiple other sites that came up in a Google search related to my problem. I know that this should be a simple fix, but I believe I am overthinking the issue.

To upload the file path into my database, I used:

<?php
    $database = "db";

    $mysqli = new mysqli("localhost", "root", "", $database);
    mysqli->select_db($database);

    $upload = rand(1000,100000)."-".$_FILES['upload']['name'];
    $uploadLocation = $_FILES['upload']['tmp_name'];

    $folder = "uploads/";

    move_uploaded_file($uploadLocation, $folder.$upload);

    $query = "INSERT INTO table (upload) VALUES ('{$uploadLocation}');

    $mysqli->query($query);
    $mysqli->close();
?>

To view the database on a separate page, I used:

<?php
    $result = mysqli_query($mysqli, "SELECT upload FROM table");

    if ($result) {
        while($row = mysqli_fetch_array($result)) {
            echo '<td><a href="upload.php">' . $row['upload'] . '</a></td>';
        }
    }

    mysqli_free_result($result);
?>

When the link to the PDF file is clicked, I used:

<?php
    $file = ' . $folder.$upload . ';
    $filename = ' . $folder.$upload . ';
    header('Content-type: application/pdf');
    header('Content-Disposition: inline; filename="' . $filename . '"');
    header('Content-Transfer-Encoding: binary');
    header('Accept-Ranges: bytes');
    @readfile($file);
?>

I believe that the problem lies within my page where the PDF is clicked. The other 2 pages work fine, and everything shows up in the database as it should. However, when you click on the PDF file link in the database, an Acrobat Reader page pops up in the browser and says that the file cannot be read. Could someone help point me in the right direction as to what I'm doing wrong?

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
nseanp
  • 23
  • 6
  • You have numerous typos in this question. Is this your real code? – user3783243 Jun 24 '19 at 15:39
  • I used this same format, but this is not my real code. – nseanp Jun 24 '19 at 15:40
  • I am new to PHP, and this is what I've put together from researching similar questions. – nseanp Jun 24 '19 at 15:50
  • @user3783243 any suggestions? I keep getting the same error, no matter what I do. The PDF Viewer displays and says "This PDF document may not be displayed correctly." It doesn't show the file at all. – nseanp Jun 25 '19 at 16:18
  • Use `error_log` or `var_dump` and look at the actual values you are using. For example the output of `$file` should be pretty clear what the issue there is. `$filename` has the same issue. Your `$query` has a syntax error and you should avoid jumping between procedural and object oriented. – user3783243 Jun 25 '19 at 16:40
  • Possible duplicate of [What is the difference between single-quoted and double-quoted strings in PHP?](https://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php) – user3783243 Jun 25 '19 at 16:41
  • Is there a good tutorial that I could follow? – nseanp Jun 25 '19 at 17:10
  • A PHP tutorial, or for which subject matter? `$file = ' . $folder.$upload . ';` should be `$file = $folder . $upload;` or `$file = $folder . '/' . $upload;` – user3783243 Jun 25 '19 at 17:15
  • @user3783243 I have tried all of your suggestions and I'm still getting the same result. I have even tried using `$file = $folder . $row['upload'];` and I'm still getting the same result. – nseanp Jun 27 '19 at 16:52
  • If you tried https://stackoverflow.com/questions/56739770/link-to-a-pdf-file-stored-in-mysql-database-using-php?noredirect=1#comment100074645_56739770 than you should update the question with the additional data. We can't help without specific data. – user3783243 Jun 27 '19 at 18:58

2 Answers2

1

I've done it. I was overthinking the issue here. I didn't even need the upload.php script above.

In the database itself, I used:

echo '<td><a href="uploads/' . $row['upload'] . '">' . $row['upload'] . '</a></td>';
nseanp
  • 23
  • 6
0

Your error is: "INSERT INTO table (upload) VALUES ('{$uploadLocation}');"

$uploadLocation make a reference the temporary name, the correct one is to store the new name you created:

INSERT INTO table (upload) VALUES ('{$upload}');