Can't get fid of the saved file after file_save_data I have been trying to get the fid of the file which my custom module is saving and every time I end up getting the error "Trying to get property of non-object in report()". I have tried file_object->id() and file_object-> fid ->value but neither are working. This is my code in .module file:

function file_write($source , $name) {

$uri =  file_default_scheme() . '://'.$name.'.txt';

$file_object = file_save_data($source, $uri, FILE_EXISTS_REPLACE);
return $file_object;
}


And this is where how I call it and try to get fid:

$file_object = file_write($source, $name);
$fid = $file_object-&gt;fid-&gt;value;


Is there anything wrong with the code?
I also tried using other methods, but no luck so far.
In Drupal 7, $file_object-&gt;fid should work.

Looking at the code you have tried ($file_object-&gt;fid-&gt;value, file_object-&gt;id()), I guess you have been mixing up Drupal 8 and Drupal 7 documentation.