I really don't know how ../file.txt should turn into /tmp/file.txt in your example, but to check if the path is an approach to another, use realpath with a simple comparison:
$path = '../foo'; $allowedPath = '/tmp/'; $path = realpath($path); if (substr($path, 0, strlen($allowedPath)) !== $allowedPath) { // path is not within allowed path! }
source share