This issue just started occurring on our app. We allow users to upload files in our forms and we create links for those files like so;

$f = \Drupal::entityTypeManager()->getStorage('file')->load($file->value);
if ($f) {
  $fileLink = [
    'title' => $f->get('filename')->value,
    'link' => \Drupal::service('file_url_generator')->generateAbsoluteString($f->get('uri')->value),
  $fileLinks[] = $fileLink;

Nothing special.

Our default download method is "Public local files served by the webserver."

The issue is that as of last week all files, regardless of mime, display as bytecode and not the file.

The url to this file is direct with extension, so its not being served from Drupal, meaning no mime issues. I can open the file perfectly fine and if you right click and save as, the file downloads and opens fine.

Appreciate the help.

  • What changed in the codebase or in the platform last week?
    – cilefen
    Oct 13, 2022 at 10:47
  • Hi @cilefen thats the thing, this was raised as an issue a long time after the previous deployment. There's a good chance this has been around for a while and went unnoticed which means it could have been caused by any number of releases.
    – Christian
    Oct 13, 2022 at 10:51
  • 1
    What is happening is that the wrong content-type HTTP header is sent. If this is custom code you are not setting the header, or you are not calling the type detection library, or you are calling the library but the library broke, or you were depending on framework or platform magic which broke.
    – cilefen
    Oct 13, 2022 at 11:38
  • 1
    Very recently Symfony Framework broke and then fixed its mime detection in BinaryFileResponse. I haven’t yet verified if this could actually affect Drupal: github.com/symfony/symfony/pull/47746
    – cilefen
    Oct 13, 2022 at 11:58
  • The issue goes away if I change the files to be temporary in the file_managed table. Nothing else changes and they load fine. I'm even using core code to generate the links now. Not sure why this was down voted.
    – Christian
    Oct 14, 2022 at 2:25

1 Answer 1


The issue was due to an implementation of hook_file_download.

We were returning a 1 in this hook to allow certain users access, but the hook needs to return the headers of the file when a user has access.

Interestingly, this code has not changed since mid 2020 so something else has caused this to be an issue.

return file_get_content_headers($file);

All fixed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.