微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如果 php 会话存在,则从 .htaccess 锁定文件夹下载文件

如何解决如果 php 会话存在,则从 .htaccess 锁定文件夹下载文件

我一直在寻找关于如何仅在存在用户会话的情况下安全地从网站下载文件的好指南。

如果用户会话不存在,则不应访问下载文件夹中的文件

因此,我假设存储文件文件夹需要由 .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);


版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。