by Darko Miletić.
I would suggest to read through http://docs.moodle.org/dev/File_API and if needed go through actual code to understand what is used and where. It is always better to use official API than do it manually.