
We can use loadByProperties method in the \Drupal\Core\Entity\EntityStorageInterface.it will help us to search the file entity by the given file URI:
/** @var \Drupal\file\FileInterface[] $files */
$files = \Drupal::entityTypeManager()
->getStorage('file')
->loadByProperties(['uri' => $uri]);
/** @var \Drupal\file\FileInterface|null $file */
$file = reset($files) ?: NULL;
In some other cases if you don't know the file URI
We can use the below code to get the URI: