function FileUploadHandler::loadByUri

Same name and namespace in other branches
  1. 10 core/modules/file/src/Upload/FileUploadHandler.php \Drupal\file\Upload\FileUploadHandler::loadByUri()

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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.