我试图在foreach循环中使用php条件,但我遇到了一些困难。我查看了其他涉及该问题的主题,但无法在我的代码中找到解决方案/解释。这是:
<?php
$directory = $_SESSION['cwd']; // current directory
$user = $_SESSION['simple_auth']['username']; // get username
$repository = gatorconf::get('repository'); // get base repertory of the file manager
$userdir = $repository.DS.'user'.DS.$user; // user's repertory
function scanDirectory($userdir = ''){
$folders = glob($userdir . '/*' , GLOB_ONLYDIR);
foreach($folders as $folder){
echo "$folder<br />"; // line where i tried different things for my condition
scanDirectory($folder);
}
}
scanDirectory($userdir);
?>
我试图用一个条件替换行echo "$folder<br />";
,以便在回到循环之前将变量$ folder与另一个变量进行比较而不是回显,就像这样:
if ($folder == $directory) {
echo "you have permissions on this folder";
}
基本上我的代码的目的是将特定的库与用户当前所在的库存进行比较,如果匹配则会显示一些html,但我认为我没有正确的方法。
感谢任何建议!
感谢您的帮助
答案 0 :(得分:1)
将当前目录作为参数提供给函数,并在浏览所有文件夹时将其传递。
<?php
$directory = $_SESSION['cwd']; // current directory
$user = $_SESSION['simple_auth']['username']; // get username
$repository = gatorconf::get('repository'); // get base repertory of the file manager
$userdir = $repository.DS.'user'.DS.$user; // user's repertory
function scanDirectory($userdir = '', $directory){
$folders = glob($userdir . '/*' , GLOB_ONLYDIR);
foreach($folders as $folder){
if ($folder == $directory) {
echo "you have permissions on this folder";
}
scanDirectory($folder, $directory);
}
}
scanDirectory($userdir, $directory);
?>