0

我正在使用 PHP 和 Javascript 制作一个小文件浏览脚本,但遇到了一个小问题。当前目录作为 GET 存储在 url 中,就像index.php?dir=/projects/jphp它在 base 中一样。然后我使用以下方法将其存储在变量$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>

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

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

有什么方法可以保持路径简单,因为上面的路径与

projects/jphp

如果您知道如何将顶部隐藏到底部,那就太好了,谢谢!我可能没有很好地解释这一点,所以让我知道你我没有道理。

4

2 回答 2

2

该功能realpath()可以满足您的需求。如果您没有得到任何结果,请检查您的文件夹权限:

The running script must have executable permissions 
on all directories in the hierarchy, otherwise 
realpath() will return FALSE.
于 2011-12-28T13:50:32.277 回答
0

我认为你可以用 替换你的链接&dir=/some/path/../&dir=/some/所以不要链接到/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>
于 2011-12-28T13:58:57.257 回答