How to add a local image file to an image field programatically I have serveral image files in the folder /sites/subsite.example.com/files/blog.dir/1/gallery1, and I need to add these files to a node programatically. I think this is the right code:  

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

  $node = Node::load(1);
  $uri = 'public://image.jpg'; &lt;--- this is my problem

  // check first if the file exists for the uri    
  $files = \Drupal::entityTypeManager()
    -&gt;getStorage('file')
    -&gt;loadByProperties(['uri' =&gt; $uri]);
  $file = reset($files);

  // if not create a file
  if (!$file) {
    $file = File::create([
      'uri' =&gt; $uri,
    ]);
    $file-&gt;save();
  }

  $node-&gt;field_image[] = [
    'target_id' =&gt; $file-&gt;id(),
    'alt' =&gt; 'Alt text',
    'title' =&gt; 'Title',
  ];

  $node-&gt;save();


What I don't know is how must be the $uri I need to put when my files are in a subdomain directory in a mulitsite drupal installation.
public:// is the Public file system path that you can set in /admin/config/media/file-system.

In your case it would probably be sites/subsite.example.com/files.

To get the image you must then use, public://blog.dir/1/gallery1/image.jpg, where public:// is the same as sites/subsite.example.com/files.