How to let users with required permission download a file via php?

前端 未结 5 1254
南旧
南旧 2021-01-03 00:27

I have a php file that acts as a gatekeeper for all the files I want people to download, who ahve sufficient privilages.

The code I use throw the file to the user i

5条回答
  •  借酒劲吻你
    2021-01-03 00:42

    Ok, having php send files of around 400Mb–10Gb is not good. You need to somehow let whatever webserver you're using actually serve the files.

    This really comes down to how secure you need it to be. The easiest solution that comes to mind (but far from the most secure) is using symbolic links with long random names that link to the original file. After a certain time the symbolic links expire and are removed. Each user get their own symbolic link (or "token") to the file they're downloading. I'm not sure how this plays out in Windows-environment, but on unix it's fairly straightforward anyway.

    Here's some pseudo code:

    if($user->isAllowedToDownload($file)){
        $token = md5($user->name . $file->name . time() . $someGoodRandomValue);
        symlink($file, $download_path . $token);
        header("Location: $download_url$token"); 
    }
    

    Then you need a cron job that cleans out old symbolic links. You also need to make sure the webserver is set to follow symbolic links, preferably only for that folder where these download tokens are created.

    So when the user maybe requests domain.com/download?file=bigfile.mp4 a symbolic link is created in the webservers public space that points to the real file outside the webservers public space. The user gets redirected to maybe domain.com/getFile/ab739babec890103bdbca72 which in turn causes the webserver to serve the file. Now it's very hard for users to try and guess what an URL is for a file, and that's the "security".

提交回复
热议问题