I think the rule of thumb in Moodle when it comes to handling its files, is to avoid touching the files directly. If you want to fetch the files belonging to a particular course, course module or plugin, then I suggest that you look at Moodle's File API.
Consider reading through the following content:
https://docs.moodle.org/dev/File_API, and this one:
They deal with how to handle Moodle files