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?