大家好,我试图在 php 上创建一个文件管理系统,但是当我尝试下载上传的图像时,它确实打开并说图像不支持
这是我的下载代码
<?php
include_once 'includes/dbh.inc.php';
if (isset($_GET['id'])) {
$id = $_GET['id'];
// fetch file to download from database
$sql = "SELECT * FROM items WHERE id = $id";
$result = mysqli_query($conn, $sql);
$file = mysqli_fetch_assoc($result);
$filepath = 'uploads/' . $file['filename'];
print_r($file);
if (file_exists($filepath)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename($filepath));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize('uploads/' . $file['filename']));
readfile('uploads/' . $file['filename']);
// Now update downloads count
$newCount = $file['downloaded'] + 1;
$updateQuery = "UPDATE items SET downloaded=$newCount WHERE id=$id";
mysqli_query($conn, $updateQuery);
exit;
}
}