Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果存在php会话,则从.htaccess锁定文件夹下载文件_Php_File_.htaccess_Session_Download - Fatal编程技术网

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

如果存在php会话,则从.htaccess锁定文件夹下载文件,php,file,.htaccess,session,download,Php,File,.htaccess,Session,Download,我一直在寻找一个很好的指南,说明只有在存在用户会话的情况下,如何安全地从网站下载文件 如果用户会话不存在,则不应访问下载文件夹中的文件。 因此,我假设存储文件的文件夹需要被.htaccess文件“锁定”吗? 或者存储在根文件夹之外?哪一个是最好的 如果有人能为我提供一个很好的指南/教程,我将不胜感激。谢谢这是我最后做的,效果很好。在我的场景中,我将文件存储在根文件夹之外 $filename= $_GET['filename']; // the file path and file you wa

我一直在寻找一个很好的指南,说明只有在存在用户会话的情况下,如何安全地从网站下载文件

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

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



看到了,也许会谢谢你,但是有没有更好的方法,或者没有什么区别?哪一个更好,将文件存储在根文件夹之外,还是存储在由.htaccess锁定的文件夹之外?:“要求我们推荐或查找书籍、工具、软件库、教程或其他非现场资源的问题不属于堆栈溢出的主题,因为它们往往会吸引固执己见的答案和垃圾邮件。”在应用程序外部存储的好处是,服务器配置不必知道应用程序。我也在Nginx上托管,其中htaccess不是一个选项,所以它也有帮助。确保您的测试不会被目录遍历漏洞攻击