Thanks to visit codestin.com
Credit goes to github.com

Skip to content

BinaryFileResponse doesn't work unless prepare is called #28237

Closed
@iluuu1994

Description

@iluuu1994

Symfony version(s) affected: 3.4.14

Description

I'm using the HttpFoundation component to stream binary files. I'm using the basic snipped underneath which produces no output. The reason for this is this line.

stream_copy_to_stream has the following signature:

int stream_copy_to_stream(resource $source, resource $dest[, int $maxlength = -1[, int $offset = 0]])

The maxlen and offset properties are only actually ever set in the prepare method. Thus Symfony passes null to both of these parameters unless prepared is called. PHP will not do anything if these properties are null:

https://3v4l.org/MtBl7

How to reproduce

$response = new BinaryFileResponse($filepath);
$response->send();

Possible Solution

The solution is to call prepare on the response. The docs do mention the prepare method but don't say that it's mandatory.

$response = new BinaryFileResponse($filepath);
$response->prepare(Request::createFromGlobals());
$response->send();

Symfony should either:

  • Be clearer in the documentation that prepare is necessary
  • Throw an error when maxlength or offset are null
  • Default to -1 and 0 as the default values

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions