我正在尝试从我的数据库中下载文件,该文件是我使用path上传的,并将文件复制到文件夹中。当我下载文件并尝试打开它时,它显示“无法加载PDF文档”。我不知道我在做什么错。有谁可以帮助我吗?谢谢大家。
这是我的上传文件代码:
$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')";
这是我的下载代码:
<?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
}
}
?>
我正在列出它/将它显示在我的桌子上
<?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();
?>
答案 0 :(得分:1)
$contactupload
只是文件名。您需要返回文件内容,而不是文件名。
更改
echo $contractupload;
到
readfile("files/contracts/$contractupload");
此外,您不应有多个Content-type:
标头。如果应该是PDF,则应为:
header("Content-type: application/pdf");