将相对路径转换为绝对路径的最佳方法是什么?

I am making a search script and want to stop any deep level search for files and folders outside of the web document root.

In case the user passes in something like ../../../../../../ I want to capture it and convert it into the absolute path that it points to, then perform some sanitation checks.

So in the example if ../../../../../ is converted to / I want to replace / with $_SERVER['DOCUMENT_ROOT'] or any other function in PHP to push the search and lock it in the web server root.

我正在制作搜索脚本,并希望停止对Web文档根目录之外的文件和文件夹的任何深层次搜索 。 p>

如果用户传递了类似 ../../../../../../ code>的内容,我想抓住它 并将其转换为它指向的绝对路径,然后执行一些卫生检查。 p>

所以在示例中如果 ../../../../。 ./ code>转换为 / code>我想用 $ _ SERVER ['DOCUMENT_ROOT'] code>或PHP中的任何其他函数替换 / code> 推送搜索并将其锁定在Web服务器根目录中。 p> div>



 2 条回答

php