New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BinaryFileResponse doesn't work unless prepare is called #28237
Comments
Hey @iluuu1994 I apologize if I'm misunderstanding you. I tried to reproduce your error, but I wasn't able to. One thing I wasn't clear on, is whether you are using the Symfony framework or just the HttpFoundation component. In the first situation, you can simply return your |
But the HttpFoundation component docs does mention |
@WackyMole When using the Symfony kernel Symfony will always calls
I'm using the |
|
@WackyMole Ok 👍 The docs do not mention that |
Well, it wasn't the error I thought it was. This works. $response = new BinaryFileResponse(__DIR__.'/Test.txt');
$response->setContentDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
'filename.txt'
);
$response->send(); |
@WackyMole Can you try it completely without a Symfony kernel? In a standalone |
Sure can. This was created as a single P.S. Do you know how many |
😂 I can reproduce my problem with your example. The file is downloaded (as the content disposition header is set correctly) but the file is empty because |
Yeah, I noticed that after I commented. Something in |
…mpty file (wackymole) This PR was merged into the 2.8 branch. Discussion ---------- [HttpFoundation] Fix unprepared BinaryFileResponse sends empty file | Q | A | ------------- | --- | Branch? | 2.8 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes, with the exception of preexisting, unrelated failures | Fixed tickets | #28237 | License | MIT | Doc PR | When you call `BinaryFileResponse#sendContent()` without first calling `prepare()` the response is sent but the contents are empty. `prepare()` properly initializes the `$maxlen` and `$offset` properties. However, `sendContent()` doesn't do any sanity checking, and so, uses the uninitialized properties. This causes `stream_copy_to_stream()` to copy empty contents and the file that is sent, to contain nothing. This change initializes the properties at definition instead of in `prepare()`. > Additionally: > - Bug fixes must be submitted against the lowest branch where they apply ~I'm not sure how early this bug exists, or how far back to go. I'll check to see if 2.7 and 2.8 are affected and report back.~ Commits ------- dba8687 Instantiate $offset and $maxlen at definition
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:The
maxlen
andoffset
properties are only actually ever set in theprepare
method. Thus Symfony passesnull
to both of these parameters unlessprepared
is called. PHP will not do anything if these properties are null:https://3v4l.org/MtBl7
How to reproduce
Possible Solution
The solution is to call
prepare
on the response. The docs do mention theprepare
method but don't say that it's mandatory.Symfony should either:
prepare
is necessarymaxlength
oroffset
are null-1
and0
as the default valuesThe text was updated successfully, but these errors were encountered: