将./转换为真实目录PHP


Covert ../ Into Real Directories PHP

我正在使用PHP和Javascript制作一个小文件浏览脚本,我遇到了一个小问题。当前目录作为GET存储在url中的index.php?dir=/projects/jphp中,当它在基目录中时。然后使用

将其存储在变量$scandir
$scandir = $_GET['dir'];

作为列在表格中的字段,以便用户可以浏览它们,文件夹被超链接如下:

<td><a style="color: red;" href="edit.php?pid=<?php echo $project['id'] ?>&dir=<?php echo $scandir . "/" . $thisfile; ?>"><?php echo $thisfile; ?></a></td>

使用遍历目录中的每个文件的foreach()循环填充变量$thisfile
当用户开始浏览文件夹和文件时,文件地址变得混乱,例如,在浏览文件一分钟后,$_GET['dir'];看起来像

projects/jphp/js/../js/../../../projects/jphp

是否有办法保持路径简单,因为上面的路径与

完全相同?
projects/jphp

如果你知道如何把顶部变成底部,那就太好了,谢谢!我可能没有解释清楚,所以请让我知道我没有说清楚。

函数realpath()执行您正在寻找的操作。如果你没有得到任何结果,检查你的文件夹权限:

The running script must have executable permissions 
on all directories in the hierarchy, otherwise 
realpath() will return FALSE.

我认为你可以用&dir=/some/代替&dir=/some/path/../这样的链接,所以不要链接到/some/path/../,而是链接到/some/,它们是相等的。

乌利希期刊指南:

,例子:

<?php
$dir = $scandir;
if( $dir=='..' ){
    if( $scandir=='' || $scandir=='/' ){
        // skip this entry using continue or something else
    }
    if( substr($scandir,-1)=='/' ){
        $dir = dirname(substr($scandir,0,-1)).'/';        
    }else{
        $dir = dirname($scandir).'/';
    }
    if( $dir=='' ){
        $dir = '/';
    }
}else{
    $dir = $scandir.'/'.$thisfile;
}
?>
<td><a style="color: red;" href="edit.php?pid=<?php echo $project['id'] ?>&dir=<?php echo $dir; ?>"><?php echo $thisfile; ?></a></td>