我想向网站上的授权用户提供.pdf
和.doc
文件。用户在登录时只能看到文件选择页面,但这并不妨碍未经授权的用户在知道完整URL的情况下查看文档。PHP来保护PDF和DOC
如何防止未经授权的用户访问这些文件?
我想向网站上的授权用户提供.pdf
和.doc
文件。用户在登录时只能看到文件选择页面,但这并不妨碍未经授权的用户在知道完整URL的情况下查看文档。PHP来保护PDF和DOC
如何防止未经授权的用户访问这些文件?
的答案很简单, @乔恩斯特林已经张贴了这个,因为我打字,但我会解释一下你
一个把你的文件,你的公共html目录之外,
例如cPanel设置
user/public_html
/public_html/download.php
user/documents/
/documents/file.doc
/documents/file.pdf
@dhh发布了一个基本download.php
php文件但是因为你想强制下载他们的东西,你可以做的就像找到并提供正确的MIME类型这里是对他的代码的扩展,以1强制下载文件的最佳方式,2允许不同的文件类型
的download.php
//check users is loged in and valid for download if not redirect them out
// YOU NEED TO ADD CODE HERE FOR THAT CHECK
// array of support file types for download script and there mimetype
$mimeTypes = array(
'doc' => 'application/msword',
'pdf' => 'application/pdf',
);
// set the file here (best of using a $_GET[])
$file = "../documents/file.doc";
// gets the extension of the file to be loaded for searching array above
$ext = explode('.', $file);
$ext = end($ext);
// gets the file name to send to the browser to force download of file
$fileName = explode("/", $file);
$fileName = end($fileName);
// opens the file for reading and sends headers to browser
$fp = fopen($file,"r") ;
header("Content-Type: ".$mimeTypes[$ext]);
header('Content-Disposition: attachment; filename="'.$fileName.'"');
// reads file and send the raw code to browser
while (! feof($fp)) {
$buff = fread($fp,4096);
echo $buff;
}
// closes file after whe have finished reading it
fclose($fp);
PS这里是MIME类型的abig列表,如果你想添加其他文件 http://www.hansenb.pdx.edu/DMKB/dict/tutorials/mime_typ.php
你可以做的是提供相当于文件的PHP代理。
将文件放在webroot之外,然后编写一个脚本来检查用户是否被允许访问。如果不是,则重定向它们,如果它们是,则设置适当的标题并输出文件数据。
您应该将所有下载内容存储在公共/用户可访问的doc根目录(当然是在您的basedir内),并添加下载脚本以在用户授权时发送下载。
这里有一些如何“发送”文件下载的例子。
$file = "ireland.jpg";
$fp = fopen($file,"r") ;
header("Content-Type: image/jpeg");
while (! feof($fp)) {
$buff = fread($fp,4096);
print $buff;
}
这为我做这份工作的支持:我置于。 pdf和一个.htaccess文件降低它在我的apache web服务器上的普通文件夹中的代码(我将它命名为“docs”)。
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
<Files /index.php>
Order Allow,Deny
Allow from all
</Files>
然后,我把代码从马丁巴克回答以上,改变了文件路径为“文档/ sample.pdf”,并将其粘贴到我的根目录下的PHP文件。而已。您现在无法访问每个网址的文件,但是如果您运行test.php,则可以下载它。
如果您不支持网络服务器E.G的用户目录,您只能访问public_html目录,而不是上面的目录,因为某些免费主机提供了这个功能 –
这正是我正在寻找的,谢谢! – Mike