PHP保护PDF和DOC [英] PHP to protect PDF and DOC

查看:152
本文介绍了PHP保护PDF和DOC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在向网站上的授权用户提供 .pdf .doc 文件。用户只能在登录时看到文件选择页面,但如果他们了解完整的URL,则不会阻止未经授权的用户查看文档。

I am trying to provide .pdf and .doc files to authorized users on a website. The user can only see the file selection page when logged in but this doesn't prevent an unauthorized user from viewing the documents if they have knowledge of the full URL.

如何我可以防止未经授权的用户访问这些文件吗?

How can I prevent unauthorized users from accessing these files?

推荐答案

答案很简单,
@Jon Stirling已发布这是因为我正在打字,但我会为您解释一点点。

the answer is quite simple, @Jon Stirling has posted this as i was typing but i will explain a little more for you

一个将您的文件放在您的公共html目录之外,

one put your files outside of your public html directory,

EG cpanel设置

user/public_html
    /public_html/download.php

user/documents/
    /documents/file.doc
    /documents/file.pdf

@dhh已发布文件php文件,您将需要正确的mime类型,这里是一个扩展到他的代码,以最好的方式1强制下载文件,和2允许不同的文件类型

@dhh has posted the file php file other that you will need the correct mime type here is a extension on to his code as to the best way to 1 force download of file, and 2 allow different filetypes

downlaod.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类型的列表
http://www.hansenb.pdx.edu/DMKB/dict/tutorials /mime_typ.php

P.S here is abig list of mime types if you want to add support for other files http://www.hansenb.pdx.edu/DMKB/dict/tutorials/mime_typ.php

这篇关于PHP保护PDF和DOC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆