The error is in these two lines:
$path2 = PHP_EOL;
$path3 = $path . $path2;
On windows, the PHP Predefined Constant PHP_EOL
has the value \r\n
. On a MAC it is \r
, and on Linux it is \n
.
The array does not contain the names of the subfolders, but the names followed by a EOL character. Therefore chdir fails.