继承我的代码
<?php
$file = $_GET['file'];
if (file_exists("uploaded_files\\" .$file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Length: ' . filesize($file));
header("Content-Disposition: attachment; filename=" . basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile("uploaded_files\\" .$file);
exit;
} else {
echo "Download failed";
}
?>
我试图让用户下载他们在我的文件夹中上传的任何文件,名为uploaded_files
答案 0 :(得分:0)
您需要更改文件位置,因为readfile()
需要文件的绝对路径
<?php
$file = $_GET['file']
$file = "uploaded_files/".$file;
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}else {
echo "Download failed";
}
?>