如何密码保护二进制文件下载? [英] How can I password protect a binary file download?

查看:62
本文介绍了如何密码保护二进制文件下载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在任意二进制文件上创建一个非常简单的 php 保护程序,用户输入密码并将文件下载到他们的计算机,但他们每次要下载时都必须输入密码.

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.

第一个答案密码保护 php 页面的简单方法,行 include("secure.html"); 似乎需要文件必须是可显示的 ascii,可由浏览器呈现.

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.

有没有一种方法可以保护二进制文件,比如 foo.bin,具有相同的简单程度(和类似的有限安全程度)?

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

推荐答案

将文件夹设置为存储文件的位置以拒绝所有,然后使用 readfile 您应该能够访问它.

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>

这篇关于如何密码保护二进制文件下载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆