@@ -406,13 +406,25 @@ represented by a PHP callable instead of a string::
406
406
});
407
407
$response->send();
408
408
409
- Downloading Files
410
- ~~~~~~~~~~~~~~~~~
409
+ .. note ::
410
+
411
+ The ``flush() `` function does not flush buffering. If ``ob_start() `` has
412
+ been called before or the ``output_buffering `` php.ini option is enabled,
413
+ you must call ``ob_flush() `` before ``flush() ``.
414
+
415
+ Additionally, PHP isn't the only layer that can buffer output. Your web
416
+ server might also buffer based on its configuration. Even more, if you
417
+ use fastcgi, buffering can't be disabled at all.
418
+
419
+ .. _component-http-foundation-serving-files :
420
+
421
+ Serving Files
422
+ ~~~~~~~~~~~~~
411
423
412
424
.. versionadded :: 2.1
413
425
The ``makeDisposition `` method was added in Symfony 2.1.
414
426
415
- When uploading a file, you must add a ``Content-Disposition `` header to your
427
+ When sending a file, you must add a ``Content-Disposition `` header to your
416
428
response. While creating this header for basic file downloads is easy, using
417
429
non-ASCII filenames is more involving. The
418
430
:method: `Symfony\\ Component\\ HttpFoundation\\ Response::makeDisposition `
@@ -424,6 +436,33 @@ abstracts the hard work behind a simple API::
424
436
425
437
$response->headers->set('Content-Disposition', $d);
426
438
439
+ .. versionadded :: 2.2
440
+ The :class: `Symfony\\ Component\\ HttpFoundation\\ BinaryFileResponse `
441
+ class was added in Symfony 2.2.
442
+
443
+ Alternatively, if you are serving a static file, you can use a
444
+ :class: `Symfony\\ Component\\ HttpFoundation\\ BinaryFileResponse `::
445
+
446
+ use Symfony\Component\HttpFoundation\BinaryFileResponse
447
+
448
+ $file = 'path/to/file.txt';
449
+ $response = new BinaryFileResponse($file);
450
+
451
+ The ``BinaryFileResponse `` will automatically handle ``Range `` and
452
+ ``If-Range `` headers from the request. It also supports ``X-Sendfile ``
453
+ (see for `Nginx `_ and `Apache `_). To make use of it, you need to determine
454
+ whether or not the ``X-Sendfile-Type `` header should be trusted and call
455
+ :method: `Symfony\\ Component\\ HttpFoundation\\ BinaryFileResponse::trustXSendfileTypeHeader `
456
+ if it should::
457
+
458
+ $response::trustXSendfileTypeHeader();
459
+
460
+ You can still set the ``Content-Type `` of the sent file, or change its ``Content-Disposition ``::
461
+
462
+ $response->headers->set('Content-Type', 'text/plain')
463
+ $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, 'filename.txt');
464
+
465
+
427
466
.. _component-http-foundation-json-response :
428
467
429
468
Creating a JSON Response
@@ -442,7 +481,8 @@ right content and headers. A JSON response might look like this::
442
481
$response->headers->set('Content-Type', 'application/json');
443
482
444
483
.. versionadded :: 2.1
445
- The :class: `Symfony\\ Component\\ HttpFoundation\\ JsonResponse ` class was added in Symfony 2.1.
484
+ The :class: `Symfony\\ Component\\ HttpFoundation\\ JsonResponse `
485
+ class was added in Symfony 2.1.
446
486
447
487
There is also a helpful :class: `Symfony\\ Component\\ HttpFoundation\\ JsonResponse `
448
488
class, which can make this even easier::
@@ -473,3 +513,5 @@ Session
473
513
The session information is in its own document: :doc: `/components/http_foundation/sessions `.
474
514
475
515
.. _Packagist : https://packagist.org/packages/symfony/http-foundation
516
+ .. _Nginx : http://wiki.nginx.org/XSendfile
517
+ .. _Apache : https://tn123.org/mod_xsendfile/
0 commit comments