You are viewing the website for BitFrame v1. BitFrame v2.0 was released on 11-May-2020 — new documentation and website will be available soon!


Learn how to return a response that forces a file download

File Download

The \BitFrame\Message\ResponseTrait provides a handy withDownload() method to force a file download. The signature for the method is as follows:

$response->withDownload(  ): ResponseInterface;

For example:

$app->get("/about", function ($request, $response, $next) {
   // client gets 'file.css' as 'test.css'
   return $response->withDownload(

Setting the second argument to a non-empty string in the withDownload() method would spoof the file name such that the client sees the file name you've supplied without actually renaming the actual file.

If the specified file cannot be found then a \BitFrame\Exception\FileNotReadableException is thrown.

This is a non-PSR standards based method, and is only available on class objects that use \BitFrame\Message\ResponseTrait. Default response objects created in BitFrame have this trait included automatically. The use of this helper method is strictly optional as it's merely meant as a shortcut to what you can just as easily accomplish without using it.


Let us know if you have something to say or add