Get contents of temporary uploaded file I've uploaded a file in a custom form and I can retrieve the file instance like this:

  $fid = $form_state-&gt;getValue('file')[0];
  $file = File::load($fid);


How can I get the contents of the file?

kint(file_get_contents($file-&gt;url()));


Returns false. If I visit the link manually, it gets downloaded.
You can't get the file from $form_state-&gt;getValue(). You have to upload the file first to the file table as temporary file with file_save_upload(). Then you can load the file content locally from $file-&gt;getFileUri().

  public function submitForm(array &amp;$form, FormStateInterface $form_state) {

    $validators = ['file_validate_extensions' =&gt; ['txt']];
    if ($file = file_save_upload('file', $validators, FALSE, 0)) {
      $data = file_get_contents($file-&gt;getFileUri());
      drupal_set_message($data);
    }

  }