dthh5038 2009-08-26 08:37
浏览 62
已采纳

列出文件夹内容:如何防止用户超出授权目录?

i'm working on a script to allow users to browse a given directory, which is not the directory this file is sitting in, but set in a variable.

 define('FOLDER', '../_files/');

Now, the rendred html allows to navigate subfolders inside that folder. I use a "dir" GET variable to tell my script which subfolder's content to display and a ".." link allowing to go upwards, using that same dir variable.

I've set a check that if $_GET['dir'] is equals to FOLDER, it should not display that ".." link. But it's easy enough to mess with that variable sitting in the url and wherever i do that, my script allows to browse above the authorized folder. Not exactly a safe situation...

So i'm thinking i should check the full local path of the authorized directory against the requested directory and if the latter is not inside the authorized one, not display the "..".

But i don't know how to do that. Any hints or pointer would be appreciated. Thanks

  • 写回答

2条回答 默认 最新

  • dongxie3352 2009-08-26 08:45
    关注

    You might want to look at realpath().

    $foo = realpath($foo);
    if (substr($foo, 0, 8) != '/my/path') {
        return false;
    }
    

    ...or something like that.

    Copied from this answer, since I think this question is better.

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

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog