class BinaryFileResponse extends Response

BinaryFileResponse represents an HTTP response delivering a file.

Constants

HTTP_CONTINUE

HTTP_SWITCHING_PROTOCOLS

HTTP_PROCESSING

HTTP_OK

HTTP_CREATED

HTTP_ACCEPTED

HTTP_NON_AUTHORITATIVE_INFORMATION

HTTP_NO_CONTENT

HTTP_RESET_CONTENT

HTTP_PARTIAL_CONTENT

HTTP_MULTI_STATUS

HTTP_ALREADY_REPORTED

HTTP_IM_USED

HTTP_MULTIPLE_CHOICES

HTTP_MOVED_PERMANENTLY

HTTP_FOUND

HTTP_SEE_OTHER

HTTP_NOT_MODIFIED

HTTP_USE_PROXY

HTTP_RESERVED

HTTP_TEMPORARY_REDIRECT

HTTP_PERMANENTLY_REDIRECT

HTTP_BAD_REQUEST

HTTP_UNAUTHORIZED

HTTP_PAYMENT_REQUIRED

HTTP_FORBIDDEN

HTTP_NOT_FOUND

HTTP_METHOD_NOT_ALLOWED

HTTP_NOT_ACCEPTABLE

HTTP_PROXY_AUTHENTICATION_REQUIRED

HTTP_REQUEST_TIMEOUT

HTTP_CONFLICT

HTTP_GONE

HTTP_LENGTH_REQUIRED

HTTP_PRECONDITION_FAILED

HTTP_REQUEST_ENTITY_TOO_LARGE

HTTP_REQUEST_URI_TOO_LONG

HTTP_UNSUPPORTED_MEDIA_TYPE

HTTP_REQUESTED_RANGE_NOT_SATISFIABLE

HTTP_EXPECTATION_FAILED

HTTP_I_AM_A_TEAPOT

HTTP_MISDIRECTED_REQUEST

HTTP_UNPROCESSABLE_ENTITY

HTTP_LOCKED

HTTP_FAILED_DEPENDENCY

HTTP_RESERVED_FOR_WEBDAV_ADVANCED_COLLECTIONS_EXPIRED_PROPOSAL

HTTP_UPGRADE_REQUIRED

HTTP_PRECONDITION_REQUIRED

HTTP_TOO_MANY_REQUESTS

HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE

HTTP_UNAVAILABLE_FOR_LEGAL_REASONS

HTTP_INTERNAL_SERVER_ERROR

HTTP_NOT_IMPLEMENTED

HTTP_BAD_GATEWAY

HTTP_SERVICE_UNAVAILABLE

HTTP_GATEWAY_TIMEOUT

HTTP_VERSION_NOT_SUPPORTED

HTTP_VARIANT_ALSO_NEGOTIATES_EXPERIMENTAL

HTTP_INSUFFICIENT_STORAGE

HTTP_LOOP_DETECTED

HTTP_NOT_EXTENDED

HTTP_NETWORK_AUTHENTICATION_REQUIRED

Properties

ResponseHeaderBag $headers from Response
static array $statusTexts Status codes translation table. from Response

Methods

__construct(SplFileInfo|string $file, int $status = 200, array $headers = array(), bool $public = true, null|string $contentDisposition = null, bool $autoEtag = false, bool $autoLastModified = true)

No description

static Response
create(SplFileInfo|string $file = null, int $status = 200, array $headers = array(), bool $public = true, null|string $contentDisposition = null, bool $autoEtag = false, bool $autoLastModified = true)

No description

string
__toString()

Returns the Response as an HTTP string.

from Response
__clone()

Clones the current Response instance.

from Response
$this
prepare(Request $request)

Prepares the Response before it is sent to the client.

$this
sendHeaders()

Sends HTTP headers.

from Response
$this
sendContent()

Sends the file.

$this
send()

Sends HTTP headers and content.

from Response
$this
setContent(mixed $content)

Sets the response content.

string
getContent()

Gets the current response content.

$this
setProtocolVersion(string $version)

Sets the HTTP protocol version (1.0 or 1.1).

from Response
string
getProtocolVersion()

Gets the HTTP protocol version.

from Response
$this
setStatusCode(int $code, mixed $text = null)

Sets the response status code.

from Response
int
getStatusCode()

Retrieves the status code for the current web response.

from Response
$this
setCharset(string $charset)

Sets the response charset.

from Response
string
getCharset()

Retrieves the response charset.

from Response
bool
isCacheable()

Returns true if the response is worth caching under any circumstance.

from Response
bool
isFresh()

Returns true if the response is "fresh".

from Response
bool
isValidateable()

Returns true if the response includes headers that can be used to validate the response with the origin server using a conditional GET request.

from Response
$this
setPrivate()

Marks the response as "private".

from Response
$this
setPublic()

Marks the response as "public".

from Response
bool
mustRevalidate()

Returns true if the response must be revalidated by caches.

from Response
getDate()

Returns the Date header as a DateTime instance.

from Response
$this
setDate(DateTime $date)

Sets the Date header.

from Response
int
getAge()

Returns the age of the response.

from Response
$this
expire()

Marks the response stale by setting the Age header to be equal to the maximum age of the response.

from Response
DateTime|null
getExpires()

Returns the value of the Expires header as a DateTime instance.

from Response
$this
setExpires(DateTime $date = null)

Sets the Expires HTTP header with a DateTime instance.

from Response
int|null
getMaxAge()

Returns the number of seconds after the time specified in the response's Date header when the response should no longer be considered fresh.

from Response
$this
setMaxAge(int $value)

Sets the number of seconds after which the response should no longer be considered fresh.

from Response
$this
setSharedMaxAge(int $value)

Sets the number of seconds after which the response should no longer be considered fresh by shared caches.

from Response
int|null
getTtl()

Returns the response's time-to-live in seconds.

from Response
$this
setTtl(int $seconds)

Sets the response's time-to-live for shared caches.

from Response
$this
setClientTtl(int $seconds)

Sets the response's time-to-live for private/client caches.

from Response
DateTime|null
getLastModified()

Returns the Last-Modified HTTP header as a DateTime instance.

from Response
$this
setLastModified(DateTime $date = null)

Sets the Last-Modified HTTP header with a DateTime instance.

from Response
string|null
getEtag()

Returns the literal value of the ETag HTTP header.

from Response
$this
setEtag(string|null $etag = null, bool $weak = false)

Sets the ETag value.

from Response
$this
setCache(array $options)

Sets the response's cache headers (validation and/or expiration).

from Response
$this
setNotModified()

Modifies the response so that it conforms to the rules defined for a 304 status code.

from Response
bool
hasVary()

Returns true if the response includes a Vary header.

from Response
array
getVary()

Returns an array of header names given in the Vary header.

from Response
$this
setVary(string|array $headers, bool $replace = true)

Sets the Vary header.

from Response
bool
isNotModified(Request $request)

Determines if the Response validators (ETag, Last-Modified) match a conditional value specified in the Request.

from Response
bool
isInvalid()

Is response invalid?

from Response
bool
isInformational()

Is response informative?

from Response
bool
isSuccessful()

Is response successful?

from Response
bool
isRedirection()

Is the response a redirect?

from Response
bool
isClientError()

Is there a client error?

from Response
bool
isServerError()

Was there a server side error?

from Response
bool
isOk()

Is the response OK?

from Response
bool
isForbidden()

Is the response forbidden?

from Response
bool
isNotFound()

Is the response a not found error?

from Response
bool
isRedirect(string $location = null)

Is the response a redirect of some form?

from Response
bool
isEmpty()

Is the response empty?

from Response
static 
closeOutputBuffers(int $targetLevel, bool $flush)

Cleans or flushes output buffers up to target level.

from Response
$this
setFile(SplFileInfo|string $file, string $contentDisposition = null, bool $autoEtag = false, bool $autoLastModified = true)

Sets the file to stream.

getFile()

Gets the file.

setAutoLastModified()

Automatically sets the Last-Modified header according the file modification date.

setAutoEtag()

Automatically sets the ETag header according to the checksum of the file.

$this
setContentDisposition(string $disposition, string $filename = '', string $filenameFallback = '')

Sets the Content-Disposition header with the given filename.

static 
trustXSendfileTypeHeader()

Trust X-Sendfile-Type header.

$this
deleteFileAfterSend(bool $shouldDelete)

If this is set to true, the file will be unlinked after the request is send Note: If the X-Sendfile header is used, the deleteFileAfterSend setting will not be used.

Details

at line line 47
__construct(SplFileInfo|string $file, int $status = 200, array $headers = array(), bool $public = true, null|string $contentDisposition = null, bool $autoEtag = false, bool $autoLastModified = true)

Parameters

SplFileInfo|string $file The file to stream
int $status The response status code
array $headers An array of response headers
bool $public Files are public by default
null|string $contentDisposition The type of Content-Disposition to set automatically with the filename
bool $autoEtag Whether the ETag header should be automatically set
bool $autoLastModified Whether the Last-Modified header should be automatically set

at line line 69
static Response create(SplFileInfo|string $file = null, int $status = 200, array $headers = array(), bool $public = true, null|string $contentDisposition = null, bool $autoEtag = false, bool $autoLastModified = true)

Parameters

SplFileInfo|string $file The file to stream
int $status The response status code
array $headers An array of response headers
bool $public Files are public by default
null|string $contentDisposition The type of Content-Disposition to set automatically with the filename
bool $autoEtag Whether the ETag header should be automatically set
bool $autoLastModified Whether the Last-Modified header should be automatically set

Return Value

Response

in Response at line line 239
string __toString()

Returns the Response as an HTTP string.

The string representation of the Response is the same as the one that will be sent to the client only if the prepare() method has been called before.

Return Value

string The Response as an HTTP string

See also

prepare()

in Response at line line 250
__clone()

Clones the current Response instance.

at line line 185
$this prepare(Request $request)

Prepares the Response before it is sent to the client.

This method tweaks the Response to ensure that it is compliant with RFC 2616. Most of the changes are based on the Request that is "associated" with this Response.

Parameters

Request $request A Request instance

Return Value

$this

in Response at line line 328
$this sendHeaders()

Sends HTTP headers.

Return Value

$this

at line line 290
$this sendContent()

Sends the file.

{@inheritdoc}

Return Value

$this

in Response at line line 379
$this send()

Sends HTTP headers and content.

Return Value

$this

at line line 320
$this setContent(mixed $content)

Sets the response content.

Valid types are strings, numbers, null, and objects that implement a __toString() method.

Parameters

mixed $content Content that can be cast to string

Return Value

$this

Exceptions

UnexpectedValueException

at line line 332
string getContent()

Gets the current response content.

Return Value

string Content

in Response at line line 434
$this setProtocolVersion(string $version)

Sets the HTTP protocol version (1.0 or 1.1).

Parameters

string $version The HTTP protocol version

Return Value

$this

in Response at line line 448
string getProtocolVersion()

Gets the HTTP protocol version.

Return Value

string The HTTP protocol version

in Response at line line 468
$this setStatusCode(int $code, mixed $text = null)

Sets the response status code.

If the status text is null it will be automatically populated for the known status codes and left empty otherwise.

Parameters

int $code HTTP status code
mixed $text HTTP status text

Return Value

$this

Exceptions

InvalidArgumentException When the HTTP status code is not valid

in Response at line line 499
int getStatusCode()

Retrieves the status code for the current web response.

Return Value

int Status code

in Response at line line 513
$this setCharset(string $charset)

Sets the response charset.

Parameters

string $charset Character set

Return Value

$this

in Response at line line 527
string getCharset()

Retrieves the response charset.

Return Value

string Character set

in Response at line line 545
bool isCacheable()

Returns true if the response is worth caching under any circumstance.

Responses marked "private" with an explicit Cache-Control directive are considered uncacheable.

Responses with neither a freshness lifetime (Expires, max-age) nor cache validator (Last-Modified, ETag) are considered uncacheable.

Return Value

bool true if the response is worth caching, false otherwise

in Response at line line 569
bool isFresh()

Returns true if the response is "fresh".

Fresh responses may be served from cache without any interaction with the origin. A response is considered fresh when it includes a Cache-Control/max-age indicator or Expires header and the calculated age is less than the freshness lifetime.

Return Value

bool true if the response is fresh, false otherwise

in Response at line line 582
bool isValidateable()

Returns true if the response includes headers that can be used to validate the response with the origin server using a conditional GET request.

Return Value

bool true if the response is validateable, false otherwise

in Response at line line 596
$this setPrivate()

Marks the response as "private".

It makes the response ineligible for serving other clients.

Return Value

$this

in Response at line line 613
$this setPublic()

Marks the response as "public".

It makes the response eligible for serving other clients.

Return Value

$this

in Response at line line 633
bool mustRevalidate()

Returns true if the response must be revalidated by caches.

This method indicates that the response must not be served stale by a cache in any circumstance without first revalidating with the origin. When present, the TTL of the response should not be overridden to be greater than the value provided by the origin.

Return Value

bool true if the response must be revalidated by a cache, false otherwise

in Response at line line 647
DateTime getDate()

Returns the Date header as a DateTime instance.

Return Value

DateTime A \DateTime instance

Exceptions

RuntimeException When the header is not parseable

in Response at line line 670
$this setDate(DateTime $date)

Sets the Date header.

Parameters

DateTime $date A \DateTime instance

Return Value

$this

in Response at line line 685
int getAge()

Returns the age of the response.

Return Value

int The age of the response in seconds

in Response at line line 699
$this expire()

Marks the response stale by setting the Age header to be equal to the maximum age of the response.

Return Value

$this

in Response at line line 715
DateTime|null getExpires()

Returns the value of the Expires header as a DateTime instance.

Return Value

DateTime|null A DateTime instance or null if the header does not exist

in Response at line line 736
$this setExpires(DateTime $date = null)

Sets the Expires HTTP header with a DateTime instance.

Passing null as value will remove the header.

Parameters

DateTime $date A \DateTime instance or null to remove the header

Return Value

$this

in Response at line line 760
int|null getMaxAge()

Returns the number of seconds after the time specified in the response's Date header when the response should no longer be considered fresh.

First, it checks for a s-maxage directive, then a max-age directive, and then it falls back on an expires header. It returns null when no maximum age can be established.

Return Value

int|null Number of seconds

in Response at line line 786
$this setMaxAge(int $value)

Sets the number of seconds after which the response should no longer be considered fresh.

This methods sets the Cache-Control max-age directive.

Parameters

int $value Number of seconds

Return Value

$this

in Response at line line 804
$this setSharedMaxAge(int $value)

Sets the number of seconds after which the response should no longer be considered fresh by shared caches.

This methods sets the Cache-Control s-maxage directive.

Parameters

int $value Number of seconds

Return Value

$this

in Response at line line 824
int|null getTtl()

Returns the response's time-to-live in seconds.

It returns null when no freshness information is present in the response.

When the responses TTL is <= 0, the response may not be served from cache without first revalidating with the origin.

Return Value

int|null The TTL in seconds

in Response at line line 842
$this setTtl(int $seconds)

Sets the response's time-to-live for shared caches.

This method adjusts the Cache-Control/s-maxage directive.

Parameters

int $seconds Number of seconds

Return Value

$this

in Response at line line 860
$this setClientTtl(int $seconds)

Sets the response's time-to-live for private/client caches.

This method adjusts the Cache-Control/max-age directive.

Parameters

int $seconds Number of seconds

Return Value

$this

in Response at line line 876
DateTime|null getLastModified()

Returns the Last-Modified HTTP header as a DateTime instance.

Return Value

DateTime|null A DateTime instance or null if the header does not exist

Exceptions

RuntimeException When the HTTP header is not parseable

in Response at line line 892
$this setLastModified(DateTime $date = null)

Sets the Last-Modified HTTP header with a DateTime instance.

Passing null as value will remove the header.

Parameters

DateTime $date A \DateTime instance or null to remove the header

Return Value

$this

in Response at line line 912
string|null getEtag()

Returns the literal value of the ETag HTTP header.

Return Value

string|null The ETag HTTP header or null if it does not exist

in Response at line line 927
$this setEtag(string|null $etag = null, bool $weak = false)

Sets the ETag value.

Parameters

string|null $etag The ETag unique identifier or null to remove the header
bool $weak Whether you want a weak ETag or not

Return Value

$this

in Response at line line 955
$this setCache(array $options)

Sets the response's cache headers (validation and/or expiration).

Available options are: etag, lastmodified, maxage, s_maxage, private, and public.

Parameters

array $options An array of cache options

Return Value

$this

Exceptions

InvalidArgumentException

in Response at line line 1008
$this setNotModified()

Modifies the response so that it conforms to the rules defined for a 304 status code.

This sets the status, removes the body, and discards any headers that MUST NOT be included in 304 responses.

Return Value

$this

See also

http://tools.ietf.org/html/rfc2616#section-10.3.5

in Response at line line 1028
bool hasVary()

Returns true if the response includes a Vary header.

Return Value

bool true if the response includes a Vary header, false otherwise

in Response at line line 1040
array getVary()

Returns an array of header names given in the Vary header.

Return Value

array An array of Vary names

in Response at line line 1064
$this setVary(string|array $headers, bool $replace = true)

Sets the Vary header.

Parameters

string|array $headers
bool $replace Whether to replace the actual value or not (true by default)

Return Value

$this

in Response at line line 1084
bool isNotModified(Request $request)

Determines if the Response validators (ETag, Last-Modified) match a conditional value specified in the Request.

If the Response is not modified, it sets the status code to 304 and removes the actual content by calling the setNotModified() method.

Parameters

Request $request A Request instance

Return Value

bool true if the Response validators match the Request, false otherwise

in Response at line line 1118
bool isInvalid()

Is response invalid?

Return Value

bool

See also

http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

in Response at line line 1130
bool isInformational()

Is response informative?

Return Value

bool

in Response at line line 1142
bool isSuccessful()

Is response successful?

Return Value

bool

in Response at line line 1154
bool isRedirection()

Is the response a redirect?

Return Value

bool

in Response at line line 1166
bool isClientError()

Is there a client error?

Return Value

bool

in Response at line line 1178
bool isServerError()

Was there a server side error?

Return Value

bool

in Response at line line 1190
bool isOk()

Is the response OK?

Return Value

bool

in Response at line line 1202
bool isForbidden()

Is the response forbidden?

Return Value

bool

in Response at line line 1214
bool isNotFound()

Is the response a not found error?

Return Value

bool

in Response at line line 1228
bool isRedirect(string $location = null)

Is the response a redirect of some form?

Parameters

string $location

Return Value

bool

in Response at line line 1240
bool isEmpty()

Is the response empty?

Return Value

bool

in Response at line line 1255
static closeOutputBuffers(int $targetLevel, bool $flush)

Cleans or flushes output buffers up to target level.

Resulting level can be greater than target level if a non-removable buffer has been encountered.

Parameters

int $targetLevel The target output buffering level
bool $flush Whether to flush or clean the buffers

at line line 86
$this setFile(SplFileInfo|string $file, string $contentDisposition = null, bool $autoEtag = false, bool $autoLastModified = true)

Sets the file to stream.

Parameters

SplFileInfo|string $file The file to stream
string $contentDisposition
bool $autoEtag
bool $autoLastModified

Return Value

$this

Exceptions

FileException

at line line 122
File getFile()

Gets the file.

Return Value

File The file to stream

at line line 130
setAutoLastModified()

Automatically sets the Last-Modified header according the file modification date.

at line line 140
setAutoEtag()

Automatically sets the ETag header according to the checksum of the file.

at line line 156
$this setContentDisposition(string $disposition, string $filename = '', string $filenameFallback = '')

Sets the Content-Disposition header with the given filename.

Parameters

string $disposition ResponseHeaderBag::DISPOSITIONINLINE or ResponseHeaderBag::DISPOSITIONATTACHMENT
string $filename Optionally use this UTF-8 encoded filename instead of the real name of the file
string $filenameFallback A fallback filename, containing only ASCII characters. Defaults to an automatically encoded filename

Return Value

$this

at line line 340
static trustXSendfileTypeHeader()

Trust X-Sendfile-Type header.

at line line 353
$this deleteFileAfterSend(bool $shouldDelete)

If this is set to true, the file will be unlinked after the request is send Note: If the X-Sendfile header is used, the deleteFileAfterSend setting will not be used.

Parameters

bool $shouldDelete

Return Value

$this