I'm trying to get all files from a main directory and from all its subdirectories with this PHP code:
function allFiles($dir) {
    $files = [];
    foreach(glob($dir.'*') as $fileOrDir) {
        if(is_dir($fileOrDir)) {
            $files = array_merge($files, allFiles($fileOrDir));
        } else {
            $files[] = $fileOrDir; 
        }
    }
    return $files;
}
$allFiles = allFiles($_SERVER['DOCUMENT_ROOT'].'/contents/');
foreach($allFiles as $file) {
    echo $file.'<br>';
}
But I get the error 500 Internal Server Error.
As I see a problem is in the line
$files = array_merge($files, allFiles($fileOrDir));
because the code without this line works without any errors
But what's the problem?
 
    