-2

I have a function that is supposed to compare all the folders/subfolders/subsubfolers/etc.. of a specific repertory with the directory the user is currently on. And if there is a match, it is supposed tu return true.

This function allows me to display different functionnalities for the user depending on weither he has the right permissions on the folder/subfolder he is currently on.

However 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 :

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 :

( The text in bold being echo($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. )

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

I have been told that it may be caused by me disregarding the return value of the recursive call but I don't really know what that means.

Also I noticed that echo($folder); is displaying me the same paths twice, which is not intended, maybe this is linked to my main issue ?

Any help to fix this function would be really appreaciated !

Thanks a lot.

apatik
  • 383
  • 4
  • 17

1 Answers1

2

You should return the result of your recursive call to scanDirectory within your scanDirectory function:

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;
        }

        $scan_result = scanDirectory($folder, $directory);

        if($scan_result) {
           return true;
        }
    }
    return false;
}

scanDirectory($userdir, $directory);
?>

<?php if ($getprivileges == true): ?>
<p>Permissions granted.<p>
<?php endif; ?>
David Behler
  • 179
  • 1
  • 2
  • 8
  • 1
    I think you should only return if the call to `scanDirectory()` returned `true`. – Axel Nov 05 '15 at 11:11
  • Your code worked, thanks a lot for your answer ! Any idea about the echo($folders); displaying twice the same paths ? – apatik Nov 05 '15 at 11:21