Example of hook_file_download How do I restrict the access to certain files based on their content type? I have the download section for registered users, and I want to prevent them to post links somewhere outside my site.

Is implementing hook_file_download() sufficient?

I tried this code and it's enough for my purpose, but the documentation of hook_file_download() doesn't help much.

function customize_file_download($uri) {
  global $user;
  if (!array_intersect(array('administrator','editor','expert','verified'), array_values($user-&gt;roles))) {
    return −1;
   }
}
There is a couple of hooks in drupal 7 to restrict access to files.

One thing you have to realized is that these access hooks are only going to work if your download method is private, and the files are in your private path.

As you mention in your question there is a hook_file_download that should work in your case (assuming the file is private). This is not going to work if the file, someone is linking to, is somewhere public such as sites/default/files/abc123.txt.

If you happened to be uploading these private files in a field, then hook_file_download_access would seem more appropriate. This will give you the field that the file belongs to and also the related entity that this field is being used on so you should be able to do more complicated logic such as calling the the appropriate access function to check if the user has access to the field instead of having a hard-coded list in the code.