PHP,MySQL - 从数据库下载上传的文件时出现问题


PHP, MySQL - Trouble downloading uploaded file from database

我在下载以前上传到数据库的文件时遇到问题,而不是下载该文件,计算机下载一个"下载.php"文件,这是要下载的脚本。我正在使用某种标识符,例如"download.php?id=1"来选择和下载特定文件,是的,我知道这段代码可能已被弃用并且不安全。

这是下载.php我输入:

<?php
session_start();
include("connect.php");
$namatemp=$_SESSION['nama'];
$nistemp=$_SESSION['nislogin'];
$kelastemp=$_SESSION['kelas'];
$mapeltemp=$_SESSION['mapeltemp'];
$chapteridtemp=$_SESSION['chapteridtemp'];
$query=("SELECT mime, name, size, data FROM file WHERE uploader='$namatemp' AND chapter='$chapteridtemp')");
$result=mysql_query($query);
$row=mysql_fetch_array($result);
if($row=1) {
header('Content-Type: '.$row['mime']);
header('Content-Length: '.$row['size']);
header('Content-Disposition: attachment; filename='.$row['name']);
echo $row['name'];
} else {
    echo "You have not uploaded such file."; 
}
?>

您可能需要添加:

readfile($row['name']);

然而,我认为您最好检查该页面是否存在文件以重定向到下载页面,如果不存在,则打印错误,因为我不确定标头是否可以嵌套在 if 语句中。当然,我可能错了,因为这样说而被否决。