Reputation: 383
I'm trying to use a php condition inside a foreach loop but i'm having some difficulties. I looked up in the other threads dealing with that but couldn't find a solution/explanation to apply in my code. Here it is :
<?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);
?>
I'm trying to replace the line echo "$folder<br />";
by a condition so that instead of being echo'd the variable $folder would be compared to another variable before going back into the loop, like this :
if ($folder == $directory) {
echo "you have permissions on this folder";
}
Basically the purpose of my code is to compare specific repertories with the repertory the user is currently in, and if there is a match then some html will be displayed, but I think i don't have the right approach.
Any suggestions is appreciated !
Thanks for the help
Upvotes: 1
Views: 106
Reputation: 319
Give the current directory as a parameter to the function and pass it on as you go through all folders.
<?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);
?>
Upvotes: 1