Following the answer https://stackoverflow.com/a/4205278/10329981
$basepath = '/foo/bar/baz/';
$realBase = realpath($basepath);
$userpath = $basepath . $_GET['path'];
$realUserPath = realpath($userpath);
if ($realUserPath === false || strpos($realUserPath, $realBase) !== 0) {
//Directory Traversal!
} else {
//Good path!
}
How can you make sure a non existing path is not a path traversal. Say a function creates a new path for a file that is uploaded by the user, this directory doesnt exist in the system yet, how can I make sure that this file does not contain any path traversal. I cant use realPath() as it will return false since directory doesnt exist.