hook_file_download($filepath)
contributions/docs/developer/hooks/file.php, line 203
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 file_download() @see upload_file_download()
$filepath String of the file's path.
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.
| Name | Description |
|---|---|
| Hooks | Allow modules to interact with the Drupal core. |
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,
);
}
}