How can I password protect a binary file download?

青春壹個敷衍的年華 提交于 2019-12-08 01:54:28

问题


I would like to create a very simple php protector on an arbitrary binary file, where the user enters a password and the file is downloaded to their computer, but they must enter the password each time they want to download.

In the first answer to the question Easy way to password-protect php page, the line include("secure.html"); seems to require that the file has to be displayable ascii, renderable by the browser.

Is there a way to protect a binary file, say foo.bin with the same level of simplicity (and similar limited degree of security)?


回答1:


Set the folder to where your file is stored to deny all and then using readfile you should be able to access it.

<?php
    if (!empty($_POST)) {
        $user = $_POST['user'];
        $pass = $_POST['pass'];

        if($user == "admin"
        && $pass == "admin")
        {
            $file = 'path/to/file';

            if (file_exists($file)) {
                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');
                header('Pragma: public');
                header('Content-Length: ' . filesize($file));
                ob_clean();
                flush();
                readfile($file);
                exit;
            }
        }
    }
?>

<form method="POST" action="">
    User <input type="TEXT" name="user"></input>
    Pass <input type="TEXT" name="pass"></input>
    <input type="submit" name="submit"></input>
</form>


来源:https://stackoverflow.com/questions/16137408/how-can-i-password-protect-a-binary-file-download

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!