Try to change you code in such a way that the current folder $url
is not changed inside the loop:
// $url = $url.$element->href;
echo '</br>depth'.$depth.'</br>';
echo $url.$element->href.'</br>';
flush();
ob_flush();
$result = findLinks($url.$element->href, $depth + 1, $maxDepth);
echo '......................... '.$result;