How can I get the file id if I use the file_save_data function? I will be adding new media files in D8 programmatically, how do I determine what the fid should be for each one? also how do i put the actual file contents into the media type?
this is my code 

$file_name=$file_info['file_name'];
  $file = file_get_contents($fullpath."/".$file_name); // string
 $filesaved = file_save_data($file, 'public://'.$file_directory_name."/".$file_info['file_name')',FILE_EXISTS_REPLACE);
  $fid = ???; 
  $static_media = Media::create([
      'bundle' =&gt; 'document',
      'uid' =&gt; '1',
      'langcode' =&gt; Language::LANGCODE_DEFAULT,
      'status' =&gt; Media::PUBLISHED,
      'your_image_field_name_here' =&gt; [
        'target_id' =&gt; $fid,
        'alt' =&gt; t('foo'),
        'title' =&gt; t('bar'),
      ],
    ]);
    $static_media-&gt;save();
Try

$filesaved = file_save_data($file, 'public://'.$file_directory_name."/".$file_info['file_name']), 'FILE_EXISTS_REPLACE');
$fid = $filesaved-&gt;id; 


file->id is the id if file that you can use it as entity reference target_id (I mean file field).

and a good example how save and attach file to Node 

use \Drupal\node\Entity\Node;

// Create file object from remote URL.
$data = file_get_contents('https://www.drupal.org/files/druplicon.small_.png');
$file = file_save_data($data, 'public://druplicon.png', FILE_EXISTS_REPLACE);

// Create node object with attached file.
$node = Node::create([
  'type'        =&gt; 'article',
  'title'       =&gt; 'Druplicon test',
  'field_image' =&gt; [
    'target_id' =&gt; $file-&gt;id(),
  ],
]);
$node-&gt;save();


What if the file is already present in your local file system and you want to attach it to an existing node? Here's an example for that too:

use \Drupal\node\Entity\Node;
use \Drupal\file\Entity\File;

// Create file object from a locally copied file.
$uri  = file_unmanaged_copy('public://source.jpg', 'public://destination.jpg', FILE_EXISTS_REPLACE);
$file = File::Create([
  'uri' =&gt; $uri,
]);
$file-&gt;save();

// Load existing node and attach file.
$node = Node::load(1);
$node-&gt;field_image-&gt;setValue([
  'target_id' =&gt; $file-&gt;id(),
]);
$node-&gt;save();


And lastly, what if you want to stick a placeholder file to a node? Thanks to Drupal 8, this is too easy:

use \Drupal\node\Entity\Node;

$node = Node::create([
  'type'  =&gt; 'article',
  'title' =&gt; 'Generated image test',
]);
$node-&gt;field_image-&gt;generateSampleItems();
$node-&gt;save();