5 PHP Fatal error: Uncaught ArgumentCountError (Cannot Find)

From the error message, it appears that your prior iteration made use of the looser nature of PHP5:

Too few arguments to function printDirectory(), 3 passed … and
exactly 4 expected in …

I see that you define printDirectory() with four parameters:

  • $url
  • $root
  • $path
  • $sort

The first time you call this function, you pass all four parameters. Unfortunately, when you call it recursively (within the function itself) you only pass three of the four.

$sRtn .= printDirectory($url, $root, $path . "https://wordpress.stackexchange.com/" . $f);

Try passing the $sort variable in that recursive call.