问题描述
我一直在寻找关于如何仅在存在用户会话的情况下安全地从网站下载文件的好指南。
因此,我假设存储文件的文件夹需要由 .htaccess 文件“锁定”? 或者存储在根文件夹之外?哪个最好?
如果有人能给我指出一个很好的指南/教程,我将不胜感激。谢谢
解决方法
这就是我最终做的,效果很好。在我的场景中,我将文件存储在根文件夹之外。
$filename= $_GET['filename'];
// the file path and file you want to send inline
$path = $fileroot."/files/".$filename;
if(!file_exists($path)) {
die("There has been an error unfortunately");
}
// the file name of the download,change this if needed
$public_name = basename($path);
// get the file's mime type to send the correct content type header
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime_type = finfo_file($finfo,$path);
// header("Content-Disposition: attachment; filename=$public_name;");
//Use "attachment" instead of inline if you want direct download instead
// send the headers
header("Content-Disposition: inline; filename=$public_name;");
header("Content-Type: $mime_type");
header('Content-Length: ' . filesize($path));
readfile($path);