【问题标题】:Alternative way to hide download link隐藏下载链接的替代方法
【发布时间】:2013-02-21 16:49:10
【问题描述】:

我目前正在使用此代码隐藏下载链接,但它为某些用户提供了未完成的下载。我不知道为什么,但是太多的用户向我报告了这个问题。 我当前的代码是:

file = "../".$realFileName;
$fakeFileName= 'Upbaz.ir-'.base64_decode($_GET['ffname']);
$fp = fopen($file, 'rb');
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=$fakeFileName");
header("Content-Length: " . filesize($file));
fpassthru($fp);

有人知道隐藏下载链接的其他方法吗?

【问题讨论】:

    标签: php html upload download


    【解决方案1】:

    这个脚本将处理文件的下载,它包括一个缓冲区/不同的 ext 类型(如果你愿意的话)。隐藏链接的一个好方法是将文件放在受保护的目录中,并将链接存储在数据库中。用户看到与文件绑定的 ID 或会话,服务器会找到该文件并提供服务。

    if ($fd = fopen ($fullPath, "r")) {
        $fsize = filesize($fullPath);
        $path_parts = pathinfo($fullPath);
        $ext = strtolower($path_parts["extension"]);
        switch ($ext) {
            case "txt":
                header("Content-type: application/txt"); // add here more headers for diff. extensions
                header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
                break;
            default:
                header("Content-type: application/octet-stream");
                header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
        }
        header("Content-length: $fsize");
        header("Cache-control: private"); //use this to open files directly
        while(!feof($fd)) {
            $buffer = fread($fd, 2048);
            echo $buffer;
        }
    }
    fclose ($fd);
    

    .htaccess 来保护目录(你应该只有这些文件的目录

    deny from all
    

    以上脚本修改为你的:

    $file = "../".$realFileName;
    $fakeFileName= 'Upbaz.ir-'.base64_decode($_GET['ffname']);
    if ($fd = fopen ($file, "r")) {
            $fsize = filesize($file);
    
            header("Content-type: application/octet-stream");
            header("Content-Disposition: attachment; filename=\"$fakename\"");
    
            header("Content-length: $fsize");
            header("Cache-control: private"); //use this to open files directly
            while(!feof($fd)) {
                $buffer = fread($fd, 2048);
                echo $buffer;
            }
        }
        fclose ($fd);
    

    【讨论】:

    • 我必须在服务器上安装patchinfo?因为我现在没有
    • pathinfo 是一个内置的 PHP 函数。您真的需要为所提供的文件使用不同的名称,还是只是为了防止他们找到真正的文件?
    • 它就像我的代码没有差异......你确定我没有破坏下载吗?
    • 我会说检查您的执行时间并且文件存在但下载代码有效。
    • 它的大文件大约 2 GB 用于一些低速连接......我必须把执行时间放多少?为了安全和工作下载?
    【解决方案2】:

    增加脚本运行时间

    @set_time_limit(120); 
    

    【讨论】:

    • 我的服务器是输入时间还是执行时间?
    猜你喜欢
    • 1970-01-01
    • 2012-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多