Exactly, in my opinion Moodle official documentation is very hard to understand for a developer coming from other PHP frameworks (e.g. Joomla!, Wordpress, etc.) because it introduces in a poor way the code used for any action.
Someone suggested me to use the following code, as reported in the File API documentation at https://docs.moodle.org/dev/File_API
$fs = get_file_storage();
// Prepare file record object
$fileinfo = array(
'contextid' => $context->id, // ID of context
'component' => 'mod_mymodule', // usually = table name
'filearea' => 'myarea', // usually = table name
'itemid' => 0, // usually = ID of row in table
'filepath' => '/', // any path beginning and ending in /
'filename' => 'myfile.txt'); // any filename
// Create file containing text 'hello world'
$fs->create_file_from_string($fileinfo, 'hello world');
Can you confirm that it's the correct way to store a file on the db and to save it on moodle_data?