duandou1903 2011-11-25 02:42
浏览 8
已采纳

php文件夹和文件权限

i have some questions on how folders and files permissions work. say i have users directories outside 'protected' as below..

users
  -- usera
    -- docs
  -- userb
    -- docs
protected

i do not want user B who does not have the rights, to access anything in user A directories. also, i do not want any person to access the directories directory via url links. basically i just want users to be able to access their own directories, and no one else. how can it be done?

thanks!

  • 写回答

2条回答 默认 最新

  • dongqing6661 2011-11-25 03:01
    关注

    I answered a simular question here limiting users to subdirectories which you should be able to adjust to suit your needs, I've copied it here as well.

    Download.php

    <?php
    /** Load your user assumed $user **/
    
    $file = trim($_GET['file']);
    
    /** Sanitize file name here **/
    
    if (true === file_exists('/users/user'.$user->id.'/'.$file)) {
       //from http://php.net/manual/en/function.readfile.php
       header('Content-Description: File Transfer');
       header('Content-Type: application/octet-stream');
       header('Content-Disposition: attachment; filename="'.$file.'"');
       header('Content-Transfer-Encoding: binary');
       header('Expires: 0');
       header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
       header('Pragma: public');
       header('Content-Length: ' . filesize($file));
       ob_clean();
       flush();
       readfile($file);
       exit;
    } else {
       throw new Exception('File Not Found');
    }
    

    .htaccess To deny all direct file downloads

    deny from all
    

    You would then link to the folders by using /download.php?file=filename.ext and it would only download that file from the users directory of the current user.

    You'll want to ensure you sanitize the input file name so you're not vulnerable to directory transversal exploits.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答
  • ¥20 在本地部署CHATRWKV时遇到了AttributeError: 'str' object has no attribute 'requires_grad'