Bale Posted December 21, 2018 Share Posted December 21, 2018 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(); ?> Quote Link to comment Share on other sites More sharing options...
requinix Posted December 21, 2018 Share Posted December 21, 2018 Look at these two lines of code: header("Content-Disposition: attachment; filename=" . Urlencode($contractupload)); echo $contractupload; Now describe what $contractupload is. Quote Link to comment Share on other sites More sharing options...
Bale Posted December 21, 2018 Author Share Posted December 21, 2018 (edited) That is the name of my column (contractupload) in my database where i store the path of the file Edited December 21, 2018 by Bale Quote Link to comment Share on other sites More sharing options...
requinix Posted December 21, 2018 Share Posted December 21, 2018 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.