问题描述
我正在开发一个网站,允许客户登录并查看保存在服务器上的各种 PDF.这些 PDF 对客户端来说是唯一的,不应该被未登录的人访问.将文件放到服务器上应该不是问题,我只是不确定如何将它们提供给最终用户.
I'm working to develop a website that allows clients to log in and see various PDFs saved on the server. These PDFs will be unique to the client and should not be accessible by someone who is not logged in. Getting the files onto the server shouldn't be an issue, I'm just not sure on how to serve them to end users.
我已经通过提供来自 SQL 服务器 的数据而不是文件来实现这种事情,所以我不完全确定最有效的方法是什么.
I've implemented this kind of thing with data from SQL servers being served instead of files, so I'm not entirely sure what the most effective way to go about this.
该网站位于 LAMP 上,而我在 PHP 方面的经验最少(但如果框架或其他语言能让这变得更容易,我可以学习它).
The website is on a LAMP and my minimal experience is in PHP (but if a framework or other language would make this easier, I can learn it).
我可能有点不知所措,但我通常是这样,所以任何输入都会很棒.
I'm probably in over my head but I usually am, so any input would be great.
推荐答案
将文件放在 webroot 之外.然后使用 PHP 通过脚本传递文件.这样,没有人可以直接链接到文件并绕过您的控制.(当然要确保只有在验证用户有权检索该文件后执行此操作的脚本).
Put the files outside of the webroot. Then using PHP pass the file though a script. That way no one can link to the file directly and bypass your controls. (Naturally make sure the script that does this only after verifying the user has permission to retrieve that file).
示例 PHP:
<?php
session_start();
if (!isset($_SESSION['authenticated'])) {
exit;
}
$file = '/path/to/file/outside/www/secret.pdf';
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
?>
这篇关于为 PDF 创建安全文件托管服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!



大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)