0

I am porting a Drupal 7 module to Drupal 8; it has a call to file_transfer($uri, $headers). What's the equivalent Drupal 8 code for this?

3
  • 1
    When in doubt, check the api.drupal.org code and/or check Drupal core change records (drupal.org/list-changes/published/drupal). See drupal.org/node/1957078
    – AKS
    Commented Oct 31, 2014 at 16:49
  • @AyeshK Funny, I searched for file_transfer in change records and no results came up. Just tried again, same thing. Barinder, also check out the Symfony docs on the subject
    – Clive
    Commented Oct 31, 2014 at 16:52
  • I wouldn't be surprised if that_awesome_function() with 5-6 lines gets replaced by a 5-6 level deep namespaced 50kb symfony component the very next day ;)
    – AKS
    Commented Oct 31, 2014 at 16:54

1 Answer 1

3

It seems file_transfer was removed in favor of BinaryFileResponse.

Symfony has a Symfony\Component\HttpFoundation\BinaryFileResponse class that can be used to deliver files through PHP to the client. It also supports X-Sendfile and X-Accel-Mapping via BinaryFileResponse::trustXSendfileTypeHeader().

file_transfer() has been dropped in favor of that.

Drupal 7:

<?php return file_transfer($uri, $headers); ?>

Drupal 8:

<?php 
use Symfony\Component\HttpFoundation\BinaryFileResponse; ...
return new BinaryFileResponse($uri, 200, $headers); 
?>

When looking for exact replacements and changes in D8, check the relevant api.drupal.org doc page and change records. I find it really comprehensive and has almost every change.

On a side note, I would reevaluate the module code if it happens for you to file_trasnfer() a file. Entire menu system was replaced with Symfony components and I think the odds are, you are still using the old system if it happens to file_transfer() or use some request-response thing in the site.

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.