function file_download

Page callback: Handles private file transfers.

Call modules that implement hook_file_download() to find out if a file is accessible and what headers it should be transferred with. If one or more modules returned headers the download will start with the returned headers. If a module returns -1 an AccessDeniedHttpException will be thrown. If the file exists but no modules responded an AccessDeniedHttpException will be thrown.If the file does not exist a NotFoundHttpException will be thrown.

See also

hook_file_download()

system_menu()

Related topics

2 calls to file_download()
ConfigController::downloadExport in drupal/core/modules/config/lib/Drupal/config/Controller/ConfigController.php
Downloads a tarball of the site configuration.
image_style_deliver in drupal/core/modules/image/image.module
Page callback: Generates a derivative, given a style and image path.
1 string reference to 'file_download'
system_menu in drupal/core/modules/system/system.module
Implements hook_menu().

File

drupal/core/includes/file.inc, line 1347
API for handling file uploads and server file management.

Code

function file_download() {

  // Merge remaining path arguments into relative file path.
  $args = func_get_args();
  $scheme = array_shift($args);
  $target = implode('/', $args);
  $uri = $scheme . '://' . $target;
  if (file_stream_wrapper_valid_scheme($scheme) && file_exists($uri)) {

    // Let other modules provide headers and controls access to the file.
    $headers = module_invoke_all('file_download', $uri);
    foreach ($headers as $result) {
      if ($result == -1) {
        throw new AccessDeniedHttpException();
      }
    }
    if (count($headers)) {
      return new BinaryFileResponse($uri, 200, $headers);
    }
    throw new AccessDeniedHttpException();
  }
  throw new NotFoundHttpException();
}