PHP protect a folder

后端 未结 2 588
梦谈多话
梦谈多话 2020-12-21 02:24

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 i

相关标签:
2条回答
  • 2020-12-21 03:05

    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;
    
    0 讨论(0)
  • 2020-12-21 03:16

    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 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题