Although its exceptionally unlikely the directory separator is not "/", is there a reason you chose not to use it to reduce duplicates?
couldn't this line be:
``` $path = preg_replace('|' . DIRECTORY_SEPARATOR .'+|', DIRECTORY_SEPARATOR, $path); ```

This would also handle if the directory separator were repeated accidentally multiple times. I.E. ("path//to////file.txt" becomes "path/to/file.txt")