Skip to main content

Serving Files

This chapter describes how to serve files to the client web browser.

Streaming Files in a Symfony Controller

Preparation

You need to install the package rekalogika/file-symfony-bridge to use this feature:

composer require rekalogika/file-symfony-bridge

To send a file to the web browser, you can use FileResponse:

use Rekalogika\File\Bridge\Symfony\HttpFoundation\FileResponse;
use Rekalogika\Contracts\File\FileInterface;
use Symfony\Component\HttpFoundation\Response;

class SomeController
{
public function download(): Response
{
/** @var FileInterface $file */
$file = ...;

return new FileResponse($file);
}
}

Generate a Temporary URL to a File

Rather than creating a controller action to serve a file for every possible situation, it is more convenient to generate a temporary URL to a file.

Preparation

You need to install the package rekalogika/file-server to use this feature:

composer require rekalogika/file-server

If you are not using Symfony Flex, read the documentation of rekalogika/file-bundle and rekalogika/temporary-url-bundle to learn how to register the required bundles.

PHP Usage

Wire in the TemporaryUrlGeneratorInterface service, and use the generateUrl() method to generate a temporary URL to a file. It accepts either a FileInterface or a FilePointerInterface.

use Rekalogika\TemporaryUrl\TemporaryUrlGeneratorInterface;
use Rekalogika\File\FileInterface;
use Rekalogika\File\FilePointerInterface;

/** @var TemporaryUrlGeneratorInterface $temporaryUrlGenerator */
/** @var FileInterface|FilePointerInterface $file */

$url = $temporaryUrlGenerator->generateUrl($file);

Twig Usage

In Twig templates, you can use the temporary_url filter to generate a temporary URL to a file.

<a href="{{ file|temporary_url }}" {{ temporary_url_autoexpire() }}>
Click here to download
</a>

With images, a convenient pattern is to chain the temporary_url filter with the image_resize filter from the rekalogika/file-image package.

<img src="{{ my_image|image_resize(200)|temporary_url }}" />
info

The image_resize filter requires the rekalogika/file-image package:

composer require rekalogika/file-image

Read more in the Filtering section.

More Information

The generateUrl() method and the temporary_url Twig filter accept several options. Read the documentation of rekalogika/temporary-url-bundle to learn more.