How do I make sure a file path is within a given subdirectory?

前端 未结 3 1378
隐瞒了意图╮
隐瞒了意图╮ 2021-01-13 02:19

I want to make sure a file path set via query string does not go outside of the desired subdirectory. Right now, I am checking that:

  1. The path does not start w
3条回答
  •  爱一瞬间的悲伤
    2021-01-13 02:40

    The use of realpath should not change the path, so I use it in the following way:

    function checkPath($pathToCheck) {
        global $basepath;
        $fullpath = $basepath.'/'.$pathToCheck;
        if ($fullpath==realpath($fullpath) && is_dir($fullpath)) {
            return $fullpath;
        } else {
            error_die('path not allowed: '.htmlentities($pathToCheck));
        }
    }
    

提交回复
热议问题