PHP保护文件夹 [英] PHP protect a folder

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

问题描述

我需要保护文件夹.网站是用PHP完成的.有一个管理区域,其中包含PDF,DOC等文件.我无法使用会话变量保护这些文件. PHP中有什么方法可以保护文件夹?

I need to secure a folder. Website was done in PHP. There is an admin area with files like PDF, DOC… so; I cannot protect these files with session variable. Is there a way in PHP to protect a folder?

谢谢

推荐答案

您不能仅使用PHP对其进行保护,但是在.htaccess文件的帮助下,这是可能的.

You can't protect it using only PHP, but with the help of a .htaccess file, this is possible.

在您要保护的目录中创建一个.htaccess文件,并将其放入其中:

Create a .htaccess file in the directory you want to protect, and put this in it:

Deny from all

然后,要创建一个PHP脚本来访问文件,您可以执行以下操作:

Then, to create a PHP script to access the files, you can do something like this:

// Add user authentication code
$name = 'protected_dir/file.pdf';
$fp = fopen($name, 'rb');
header("Content-Type: application/pdf");
header("Content-Length: " . filesize($name));
fpassthru($fp);
exit;

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

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