8.3.x file.api.php hook_file_download($uri)
8.0.x file.api.php hook_file_download($uri)
8.1.x file.api.php hook_file_download($uri)
8.2.x file.api.php hook_file_download($uri)
8.4.x file.api.php hook_file_download($uri)
4.7.x core.php hook_file_download($file)
5.x core.php hook_file_download($file)
6.x core.php hook_file_download($filepath)
7.x system.api.php hook_file_download($uri)

Control access to private file downloads and specify HTTP headers.

This hook allows modules to enforce permissions on file downloads whenever Drupal is handling file download, as opposed to the web server bypassing Drupal and returning the file from a public directory. Modules can also provide headers to specify information like the file's name or MIME type.

Parameters

$uri: The URI of the file.

Return value

If the user does not have permission to access the file, return -1. If the user has permission, return an array with the appropriate headers. If the file is not controlled by the current module, the return value should be NULL.

See also

\Drupal\system\FileDownloadController::download()

Related topics

6 functions implement hook_file_download()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

config_file_download in core/modules/config/config.module
Implements hook_file_download().
editor_file_download in core/modules/editor/editor.module
Implements hook_file_download().
file_file_download in core/modules/file/file.module
Implements hook_file_download().
file_test_file_download in core/modules/file/tests/file_test/file_test.module
Implements hook_file_download().
image_file_download in core/modules/image/image.module
Implements hook_file_download().

... See full list

3 invocations of hook_file_download()
FileDownloadController::download in core/modules/system/src/FileDownloadController.php
Handles private file transfers.
ImageStyleDownloadController::deliver in core/modules/image/src/Controller/ImageStyleDownloadController.php
Generates a derivative, given a style and image path.
image_file_download in core/modules/image/image.module
Implements hook_file_download().

File

core/lib/Drupal/Core/File/file.api.php, line 31
Hooks related to the File management system.

Code

function hook_file_download($uri) {
  // Check to see if this is a config download.
  $scheme = file_uri_scheme($uri);
  $target = file_uri_target($uri);
  if ($scheme == 'temporary' && $target == 'config.tar.gz') {
    return [
      'Content-disposition' => 'attachment; filename="config.tar.gz"',
    ];
  }
}