我想制作一个网页,里面有pdf的下载选项,但我希望它的密码受到保护,也就是说,如果有人点击这个链接,他必须输入用户名和密码,如果他直接打开链接“www.example.com/~文件夹_name/abc.pdf”,那么服务器首先请求密码,然后允许下载。
编辑:我希望用户在浏览器中查看文件,而不是强制下载这里的代码
<?php
/* authentication script goes here*/
$file = 'http://example.com/folder_name/abc.pdf';
//header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
header('Content-Disposition: inline; 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));
header('Accept-Ranges: bytes');
@readfile($file);
?>但这段代码并没有在我的浏览器中打开pdf。我不希望代码依赖于浏览器使用的pdf插件
发布于 2013-09-04 05:32:10
您可以在下载设置的web文件夹中创建一个.htaccess文件,以便在任何人进入域之前,必须输入正确的用户和密码才能进入。
下面是我在设置自己的博客帖子时使用的一个.htaccess文件,但本质上您的.htaccess文件如下所示:
AuthType Basic
AuthName "restricted area"
AuthUserFile /path/to/file/directory-you-want-to-protect/.htpasswd
require valid-user您还需要创建一个.htpasswd文件,您可以在其中输入用户名和密码。密码需要使用MD5哈希加密,但您可以使用他在博客中链接到的生成器。希望这能有所帮助。
发布于 2013-09-04 05:47:20
您仍然可以使用.htaccess不让任何人直接下载您的文档并保护到该文档的链接。
.htaccess可能是这样的
RewriteRule ^([A-Za-z0-9-]+).pdf$ index.php [L,QSA]您可以使用php实现这个功能。
像这样想的人
<?php
//here you authenticate user with your script
//and then let the user download it
if (!isset($_SESSION['authenticated']))
{
header('Location: http://www.example.com/');
exit;
}
$file = 'www.example.com/~folder_name/abc.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;
?>https://stackoverflow.com/questions/18605936
复制相似问题