function BinaryFileResponseTestController::download

Download the file set in the relative_file_url query parameter.

Return value

\Symfony\Component\HttpFoundation\BinaryFileResponse The response wrapping the file content.

1 string reference to 'BinaryFileResponseTestController::download'
binary_file_response_test.routing.yml in core/modules/system/tests/modules/binary_file_response_test/binary_file_response_test.routing.yml
core/modules/system/tests/modules/binary_file_response_test/binary_file_response_test.routing.yml

File

core/modules/system/tests/modules/binary_file_response_test/src/Controller/BinaryFileResponseTestController.php, line 20

Class

BinaryFileResponseTestController
Controller routines for binary file response tests.

Namespace

Drupal\binary_file_response_test\Controller

Code

public function download(Request $request) {
    if (!$request->query
        ->has('relative_file_url')) {
        throw new BadRequestHttpException();
    }
    $relative_file_url = $request->query
        ->get('relative_file_url');
    // A relative URL for a file contains '%20' instead of spaces. A relative
    // file path contains spaces.
    $relative_file_path = rawurldecode($relative_file_url);
    // Ensure the file path does not start with a slash to prevent exploring
    // the file system root.
    $relative_file_path = ltrim($relative_file_path, '/');
    return new BinaryFileResponse($relative_file_path);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.