1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How can I get the file id if I use the file_save_data function?

Discussion in 'Programming/Internet' started by Diana, Oct 8, 2018.

  1. Diana

    Diana Guest

    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 = 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' => 'document',
    'uid' => '1',
    'langcode' => Language::LANGCODE_DEFAULT,
    'status' => Media::pUBLISHED,
    'your_image_field_name_here' => [
    'target_id' => $fid,
    'alt' => t('foo'),
    'title' => t('bar'),

    Login To add answer/comment

Share This Page