hook_file_download
- Versions
- 4.7 – 5
hook_file_download($file)- 6
hook_file_download($filepath)- 7
hook_file_download($uri)
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
@see upload_file_download()
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.
Related topics
Code
modules/system/system.api.php, line 1546
<?php
function hook_file_download($uri) {
// Check if the file is controlled by the current module.
if (!file_prepare_directory($uri)) {
$uri = FALSE;
}
$result = db_query("SELECT f.* FROM {file} f INNER JOIN {upload} u ON f.fid = u.fid WHERE uri = :uri", array('uri' => $uri));
foreach ($result as $file) {
if (!user_access('view uploaded files')) {
return -1;
}
return array(
'Content-Type' => $file->filemime,
'Content-Length' => $file->filesize,
);
}
}
?>Login or register to post comments 