class FileUploadHandler (View source)

Handles validating and creating file entities from file uploads.

Constants

DEFAULT_EXTENSIONS

The default extensions if none are provided.

Properties

protected FileSystemInterface $fileSystem

The file system service.

protected EntityTypeManagerInterface $entityTypeManager

The entity type manager.

protected StreamWrapperManagerInterface $streamWrapperManager

The stream wrapper manager.

protected EventDispatcherInterface $eventDispatcher

The event dispatcher.

protected AccountInterface $currentUser

The current user.

protected MimeTypeGuesserInterface $mimeTypeGuesser

The MIME type guesser.

protected RequestStack $requestStack

The request stack.

Methods

__construct(FileSystemInterface $fileSystem, EntityTypeManagerInterface $entityTypeManager, StreamWrapperManagerInterface $streamWrapperManager, EventDispatcherInterface $eventDispatcher, MimeTypeGuesserInterface $mimeTypeGuesser, AccountInterface $currentUser, RequestStack $requestStack)

Constructs a FileUploadHandler object.

handleFileUpload(UploadedFileInterface $uploadedFile, array $validators = [], string $destination = 'temporary://', int $replace = FileSystemInterface::EXISTS_REPLACE)

Creates a file from an upload.

bool
moveUploadedFile(UploadedFileInterface $uploadedFile, string $uri)

Move the uploaded file from the temporary path to the destination.

string
handleExtensionValidation(array $validators)

Gets the list of allowed extensions and updates the validators.

FileInterface|null
loadByUri(string $uri)

Loads the first File entity found with the specified URI.

Details

__construct(FileSystemInterface $fileSystem, EntityTypeManagerInterface $entityTypeManager, StreamWrapperManagerInterface $streamWrapperManager, EventDispatcherInterface $eventDispatcher, MimeTypeGuesserInterface $mimeTypeGuesser, AccountInterface $currentUser, RequestStack $requestStack)

Constructs a FileUploadHandler object.

Parameters

FileSystemInterface $fileSystem

The file system service.

EntityTypeManagerInterface $entityTypeManager

The entity type manager.

StreamWrapperManagerInterface $streamWrapperManager

The stream wrapper manager.

EventDispatcherInterface $eventDispatcher

The event dispatcher.

MimeTypeGuesserInterface $mimeTypeGuesser

The MIME type guesser.

AccountInterface $currentUser

The current user.

RequestStack $requestStack

The request stack.

FileUploadResult handleFileUpload(UploadedFileInterface $uploadedFile, array $validators = [], string $destination = 'temporary://', int $replace = FileSystemInterface::EXISTS_REPLACE)

Creates a file from an upload.

Parameters

UploadedFileInterface $uploadedFile

The uploaded file object.

array $validators

The validators to run against the uploaded file.

string $destination

The destination directory.

int $replace

Replace behavior when the destination file already exists:

  • FileSystemInterface::EXISTS_REPLACE - Replace the existing file.
  • FileSystemInterface::EXISTSRENAME - Append {incrementing number} until the filename is unique.
  • FileSystemInterface::EXISTS_ERROR - Throw an exception.

Return Value

FileUploadResult

The created file entity.

Exceptions

FileException
FileWriteException
FileException
FileValidationException

protected bool moveUploadedFile(UploadedFileInterface $uploadedFile, string $uri)

Move the uploaded file from the temporary path to the destination.

Allows a sub-class to override this method in order to handle raw file uploads in https://www.drupal.org/project/drupal/issues/2940383.

Parameters

UploadedFileInterface $uploadedFile

The uploaded file.

string $uri

The destination URI.

Return Value

bool

Returns FALSE if moving failed.

See also

https://www.drupal.org/project/drupal/issues/2940383

protected string handleExtensionValidation(array $validators)

Gets the list of allowed extensions and updates the validators.

This will add an extension validator to the list of validators if one is not set.

If the extension validator is set, but no extensions are specified, it means all extensions are allowed, so the validator is removed from the list of validators.

Parameters

array $validators

The file validators in use.

Return Value

string

The space delimited list of allowed file extensions.

protected FileInterface|null loadByUri(string $uri)

Loads the first File entity found with the specified URI.

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

Parameters

string $uri

The file URI.

Return Value

FileInterface|null

The first file with the matched URI if found, NULL otherwise.