dss087358 2016-02-21 03:43
浏览 78

将上载文件的访问权限仅限于Web应用程序中的原始作者

Ex: a user login to a website and uploads a profile pic. I want to restrict the access to only allow the user that uploads the pic gets to have access.

So, if a second person somehow gets the URL to the pic, he or she still can not access it because she or he doesn't have the right permission(https://example.com/profile_pic_mike_1). Right now i have it so that the user can upload a profile pic but then anyone that has the URL are able to type in the browser and see the pic. I dont want that.

This is a project for fun so all suggestions are welcome.

  • 写回答

3条回答 默认 最新

  • doushaizhen1244 2016-02-21 03:52
    关注

    You can code a PHP file that will check if user is logged in, or if users have the permission to access the file.

    I'll assume your public directory is public_html.

    For the storage of images, you'll need to store them outside of the public directory (public_html).

    Example Code (e.g. image.php?file=profile_pic_mike_1). image.php is in public_html and profile_pic_mike_1 will be in the directory that is not accessible to the public.

    <?php
    
    // checking for $_SESSION, change accordingly to your method
    session_start();
    if(!$_SESSION['logged_in']){
        session_destroy();
        header("Location:index.php");
    }
    
    // $_GET['file'] will be profile_pic_mike_1
    if(isset($_GET['file'])){
    
        $file_dir = "../";
        $file = $file_dir . $_GET["file"];
    
        // get if user has the permission
        // if yes, assign `true` to $permission ($permission = true;)
    
        if (!file_exists($file)) {
    
            echo "File not found.";
    
        } else if (!$permission) {
    
            echo "You do not have the permission to view this image.";
    
        } else {
    
            // You'll need to change the Content-type accordingly
            header("Content-type: image/png");
            readfile($file);
            exit;
    
        }
    
    }
    
    ?>
    

    List of Content-type for images: http://php.net/manual/en/function.image-type-to-mime-type.php.

    You'll need to check if the user has the permission to view the file, one way to do it by saving the permissions in a MySQL DB.

    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路