You are here

protected function FileUploadHandler::loadByUri in Drupal 10

Loads the first File entity found with the specified URI.

@todo replace with https://www.drupal.org/project/drupal/issues/3223209

Parameters

string $uri: The file URI.

Return value

\Drupal\file\FileInterface|null The first file with the matched URI if found, NULL otherwise.

1 call to FileUploadHandler::loadByUri()
FileUploadHandler::handleFileUpload in core/modules/file/src/Upload/FileUploadHandler.php
Creates a file from an upload.

File

core/modules/file/src/Upload/FileUploadHandler.php, line 339

Class

FileUploadHandler
Handles validating and creating file entities from file uploads.

Namespace

Drupal\file\Upload

Code

protected function loadByUri(string $uri) : ?FileInterface {
  $fileStorage = $this->entityTypeManager
    ->getStorage('file');

  /** @var \Drupal\file\FileInterface[] $files */
  $files = $fileStorage
    ->loadByProperties([
    'uri' => $uri,
  ]);
  if (count($files)) {
    foreach ($files as $item) {

      // Since some database servers sometimes use a case-insensitive
      // comparison by default, double check that the filename is an exact
      // match.
      if ($item
        ->getFileUri() === $uri) {
        return $item;
      }
    }
  }
  return NULL;
}