Secure files for download

后端 未结 2 730
忘掉有多难
忘掉有多难 2020-11-27 04:25

I want to have a folder, lets call it docs, that contains documents that logged in users can download. These have very sensitive information. How can I best secure the folde

相关标签:
2条回答
  • 2020-11-27 05:02

    In addition of John Conde, you can rewrite the dl url into an other not sensible folder to honeypot the user who try to overpass their access.

    And add this kind of rules in your htaccess

    Options -Indexes
    
    <files .htaccess>
      order allow,deny
      deny from all
    </files>
    
    #Add all file you want to protect except the one you share...
    <FilesMatch "\.(htaccess|htpasswd|ini|phps|fla|psd|log|sh)$">
      Order Allow,Deny
      Deny from all
    </FilesMatch>
    
    0 讨论(0)
  • 2020-11-27 05:04

    Put the files outside of the webroot. Then using PHP pass the file though a script. That way no one can link to the file directly and bypass your controls. (Naturally make sure the script that does this only after verifying the user has permission to retrieve that file).

    Sample PHP:

    <?php
        if (!isset($_SESSION['authenticated'])) {
            exit;
        }
        $file = '/path/to/file/outside/www/secret.pdf';
    
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename=' . basename($file));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        readfile($file);
        exit;
    ?>
    
    0 讨论(0)
提交回复
热议问题