How can I password protect a binary file download?

▼魔方 西西 提交于 2019-12-06 11:58:29
Matthew Camp

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