开发者

PHP protect a folder

I need to secure a folder. Website was done in PHP. There is an admi开发者_运维技巧n 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?

Thank you


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

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

Deny from all

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;


You can put your files behind the viewable area(before public_html) and with a session protected download page, download the files.

<?php
if(session_is_loggedin()){
    readfile($_GET['file']);

}
?>

Obviously, there need to be some extra changes but that is the part you requested.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜