列出文件夹/子文件夹/等等的循环无法正常工作

I'm having some troubles with loop that checks the paths of every folders/subfolders/etc... and returns true if one of them matches the path of the directory the user is currently in.

It appears that my function doesn't return true when the user is in a subfolder1 or deeper (subfolder1, subfolder2, subfolder3...)

Here is what the tree looks like :

main_repertory => folder1 => subfolder1 => subfolder2 => etc...

And here is my function :

<?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
    $getprivileges = scanDirectory($userdir, $directory);

function scanDirectory($userdir = '', $directory){
    $folders = glob($userdir . '/*' , GLOB_ONLYDIR);
    foreach($folders as $folder){
    echo($folder.'</br>');
        if (($folder == $directory && gator::checkPermissions('r')) || (gator::checkPermissions('ru')) || ($userdir == $directory && gator::checkPermissions('r'))) {
            return true;
        }
        scanDirectory($folder, $directory);
    }
    return false;
}

scanDirectory($userdir, $directory);
?>

<?php if ($getprivileges == true): ?>
<p>Permissions granted.<p>
<?php endif; ?>

To find where the problem comes from I added echo($folder.'</br>'); and echo($directory); to see myself if they were displayed, and if they were matching. This is what I got :

for Folder1 (works also in the main repertory) :

C:/wamp/www/gg/ftp/repository/user/mister/folder1 C:/wamp/www/gg/ftp/repository/user/mister/folder1 C:/wamp/www/gg/ftp/repository/user/mister/folder1 Permissions granted.

for subfolder1 and deeper it stops displaying "permissions granted" even tho the $folder and $directory match :

C:/wamp/www/gg/ftp/repository/user/mister/folder1 C:/wamp/www/gg/ftp/repository/user/mister/folder1/subfolder1 C:/wamp/www/gg/ftp/repository/user/mister/folder1 C:/wamp/www/gg/ftp/repository/user/mister/folder1/subfolder1 C:/wamp/www/gg/ftp/repository/user/mister/folder1/subfolder1

The text in bold being $directory (current directory of the user), the normal text being the one displayed by the echo($folder); and "permission granted" being the text displayed when $getprivileges is true.

So I don't understand why my function stops working from subfolder1, even tho everything looks fine..

Also (less important) I don't knwo why echo($folder); is displaying me the same paths twice ?

Any help would be greatly appreaciated, you can check my previous questions if you need more background on this function I'm trying to get working but i'm hoping I posted enough infos.

Thanks a lot.