Password protected directory and files in PHP

前端 未结 3 999
盖世英雄少女心
盖世英雄少女心 2021-02-06 16:13

Im creating a simple private page with links to some files to download. I\'ve done it with simple session management but I have a problem: if somebody click on the file-url he c

3条回答
  •  执笔经年
    2021-02-06 16:40

    I guess it's a little bit late to answer. Anyway, it may help other people.

    To protect files from direct downloads, you have to use a combinaison of PHP + .htaccess.

    Let's admit that ./downloads/ is the folder where you store files you want to be downloaded. First, you have put .htaccess in this folder.

    .htaccess in ./downloads/ :

    deny from all
    

    This will protect the folder to everybody, except scripts wich are executed on the server side.


    Here is an example of a PHP script you can write at the root directory ./

    index.php in ./ :

    
    
    
        

    This script is usable as it is. But be careful. Actually there is a major vulnerability : With this form you can download any file of the server (including a file like config.php which contains access to your database). To fix that vulnerability you can use IDs :

    if ($_GET["id"] == 1)
        $filename = "toto.pdf"
    if ($_GET["id"] == 2)
        $filename = "fish.png"
    



    It provides a good example of protecting files from direct download but not from PHP download.

提交回复
热议问题