hook_file_download

Definition

hook_file_download($filepath)
developer/hooks/file.php, line 203

Description

Control access to private file downloads and specify HTTP headers.

This hook allows modules enforce permissions on file downloads when the private file download method is selected. Modules can also provide headers to specify information like the file's name or MIME type.

See also

file_download()

@see upload_file_download()

Parameters

$filepath String of the file's path.

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.

Related topics

Namesort iconDescription
HooksAllow modules to interact with the Drupal core.

Code

<?php
function hook_file_download($filepath) {
  // Check if the file is controlled by the current module.
  $filepath = file_create_path($filepath);
  $result = db_query("SELECT f.* FROM {files} f INNER JOIN {upload} u ON f.fid = u.fid WHERE filepath = '%s'", $filepath);
  if ($file = db_fetch_object($result)) {
    if (!user_access('view uploaded files')) {
      return -1;
    }
    return array(
      'Content-Type: ' . $file->filemime,
      'Content-Length: ' . $file->filesize,
    );
  }
}
?>
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.