Why does PhpSpreadsheet fail to identify my Xlsx file in public://? I'm working on a module that imports a Xlsx file and inserts the rows to database. Everything works fine, except PhpSpreadsheet throws the error Unable to identify a reader for this file when my uploaded file is in public. This error doesn't happen when the file is in base_path(). Why is this happening? Below is my code:

public function submitForm(array &amp;$form, FormStateInterface $form_state) {
    $connection = \Drupal::database();
    $fid = $form_state-&gt;getValue("xlsx_upload");
    $input = File::load(reset($fid));
    $input-&gt;setPermanent();
    $uri = \Drupal::service('file_system')-&gt;realpath($input-&gt;getFileUri());
    //$uri = '/home/joe/file.xslx'; &lt;--- this works perfectly.
    $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReaderForFile($uri);
    $spreadsheet = $reader-&gt;load($filename);
    $dataArray = $spreadsheet-&gt;getActiveSheet()-&gt;toArray();
    $connection-&gt;truncate("mitacan")-&gt;execute();
    $connection-&gt;query("SET NAMES utf8");
    foreach ($dataArray as $dataItem) {
      $connection-&gt;insert('mitacan')
      -&gt;fields([
        'stokkodu'      =&gt; $dataItem[0],
        'logo'          =&gt; $dataItem[1],
        'resim1'        =&gt; $dataItem[2],
        'grupkodu'      =&gt; $dataItem[3],
        'grupadi'       =&gt; $dataItem[4],
        'grupindex'     =&gt; $dataItem[5],
        'paketadedi'    =&gt; $dataItem[6],
      ])
      -&gt;execute();
    }
    drupal_set_message('Excel verisi veritabanına eklendi.');
  }


EDIT: The above code throws "PHP message: Uncaught PHP Exception InvalidArgumentException: "File "" does not exist ." at File.php line 137", even though line 137 throws an exception saying it's not a file, but \dpm(is_file($uri)) returns TRUE.
Looks like PhpSpreadsheet can't read URIs, but only file paths.
You need to find the path to the file on the system.

$real_path_to_file = \Drupal::service('file_system')-&gt;realpath($file-&gt;getFileUri());


(Ideally you'd inject file_system and not call this service statically.)