Final Class Yiisoft\ResponseDownload\DownloadResponseFactory
| Inheritance | Yiisoft\ResponseDownload\DownloadResponseFactory |
|---|
Provides multiple methods for creating PSR-7 compatible response with downloadable content.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\ResponseDownload\DownloadResponseFactory | |
| sendContentAsFile() | Sends the specified content as a file to the browser. A shortcut for {@see sendStreamAsFIle()}. | Yiisoft\ResponseDownload\DownloadResponseFactory |
| sendFile() | Forms a response that sends a file to the browser. A shortcut for {@see sendStreamAsFIle()}. | Yiisoft\ResponseDownload\DownloadResponseFactory |
| sendStreamAsFile() | Forms a response that sends the specified stream as a file to the browser. | Yiisoft\ResponseDownload\DownloadResponseFactory |
| xSendFile() | Forms a response that sends existing file to a browser as a download using x-sendfile. |
Yiisoft\ResponseDownload\DownloadResponseFactory |
Constants
| Constant | Value | Description | Defined By |
|---|---|---|---|
| MIME_APPLICATION_OCTET_STREAM | 'application/octet-stream' | A MIME type value for unknown binary files. | Yiisoft\ResponseDownload\DownloadResponseFactory |
Method Details
| public mixed __construct ( \Psr\Http\Message\ResponseFactoryInterface $responseFactory, \Psr\Http\Message\StreamFactoryInterface $streamFactory ) | ||
| $responseFactory | \Psr\Http\Message\ResponseFactoryInterface |
PSR-17 compatible response factory (@see https://www.php-fig.org/psr/psr-17/#22-responsefactoryinterface). |
| $streamFactory | \Psr\Http\Message\StreamFactoryInterface |
PSR-17 compatible stream factory (@see https://www.php-fig.org/psr/psr-17/#24-streamfactoryinterface). |
public function __construct(
private readonly ResponseFactoryInterface $responseFactory,
private readonly StreamFactoryInterface $streamFactory,
) {
}
Sends the specified content as a file to the browser. A shortcut for {@see sendStreamAsFIle()}.
| public \Psr\Http\Message\ResponseInterface sendContentAsFile ( string $content, string $attachmentName, string $disposition = ContentDispositionHeader::ATTACHMENT, string|null $mimeType = null ) | ||
| $content | string |
The content to be sent. |
| $attachmentName | string |
The file name shown to the user. |
| $disposition | string |
Content disposition. Either {@see \Yiisoft\Http\ContentDispositionHeader::ATTACHMENT} or {@see \Yiisoft\Http\ContentDispositionHeader::INLINE}. Default is {@see \Yiisoft\Http\ContentDispositionHeader::ATTACHMENT}. |
| $mimeType | string|null |
The MIME type of the content. If not set, it will be guessed based on the {@see $content}. |
| return | \Psr\Http\Message\ResponseInterface |
PSR-7 compatible response (@see https://www.php-fig.org/psr/psr-7/#33-psrhttpmessageresponseinterface). |
|---|---|---|
public function sendContentAsFile(
string $content,
string $attachmentName,
string $disposition = ContentDispositionHeader::ATTACHMENT,
?string $mimeType = null,
): ResponseInterface {
return $this->sendStreamAsFile(
stream: $this->streamFactory->createStream($content),
attachmentName: $attachmentName,
disposition: $disposition,
mimeType: $mimeType ?? $this->getContentMimeType($content),
);
}
Forms a response that sends a file to the browser. A shortcut for {@see sendStreamAsFIle()}.
| public \Psr\Http\Message\ResponseInterface sendFile ( string $filePath, string|null $attachmentName = null, string $disposition = ContentDispositionHeader::ATTACHMENT, string|null $mimeType = null ) | ||
| $filePath | string |
Path to a file to send. |
| $attachmentName | string|null |
File name shown to the user. If |
| $disposition | string |
Content disposition. Either {@see \Yiisoft\Http\ContentDispositionHeader::ATTACHMENT} or {@see \Yiisoft\Http\ContentDispositionHeader::INLINE}. Default is {@see \Yiisoft\Http\ContentDispositionHeader::ATTACHMENT}. |
| $mimeType | string|null |
The MIME type of the content. If not set, it will be guessed based on the file content ({@see $filePath}). |
| return | \Psr\Http\Message\ResponseInterface |
PSR-7 compatible response (@see https://www.php-fig.org/psr/psr-7/#33-psrhttpmessageresponseinterface). |
|---|---|---|
public function sendFile(
string $filePath,
?string $attachmentName = null,
string $disposition = ContentDispositionHeader::ATTACHMENT,
?string $mimeType = null,
): ResponseInterface {
return $this->sendStreamAsFile(
stream: $this->streamFactory->createStreamFromFile($filePath),
attachmentName: $attachmentName ?? basename($filePath),
disposition: $disposition,
mimeType: $mimeType ?? $this->getFileMimeType($filePath),
);
}
Forms a response that sends the specified stream as a file to the browser.
| public \Psr\Http\Message\ResponseInterface sendStreamAsFile ( \Psr\Http\Message\StreamInterface $stream, string $attachmentName, string $disposition = ContentDispositionHeader::ATTACHMENT, string $mimeType = self::MIME_APPLICATION_OCTET_STREAM ) | ||
| $stream | \Psr\Http\Message\StreamInterface |
PSR-7 compatible stream (@see https://www.php-fig.org/psr/psr-7/#34-psrhttpmessagestreaminterface) to send. |
| $attachmentName | string |
File name shown to the user. |
| $disposition | string |
Content disposition. Either {@see \Yiisoft\Http\ContentDispositionHeader::ATTACHMENT} or {@see \Yiisoft\Http\ContentDispositionHeader::INLINE}. Default is {@see \Yiisoft\Http\ContentDispositionHeader::ATTACHMENT}. |
| $mimeType | string |
The MIME type of the content. Default is {@see \Yiisoft\ResponseDownload\MIME_APPLICATION_OCTET_STREAM}. |
| return | \Psr\Http\Message\ResponseInterface |
PSR-7 compatible response (@see https://www.php-fig.org/psr/psr-7/#33-psrhttpmessageresponseinterface). |
|---|---|---|
public function sendStreamAsFile(
StreamInterface $stream,
string $attachmentName,
string $disposition = ContentDispositionHeader::ATTACHMENT,
string $mimeType = self::MIME_APPLICATION_OCTET_STREAM,
): ResponseInterface {
$this->assertDisposition($disposition);
return $this->responseFactory->createResponse()
->withHeader(Header::CONTENT_TYPE, $mimeType)
->withHeader(
ContentDispositionHeader::name(),
ContentDispositionHeader::value($disposition, $attachmentName),
)
->withBody($stream);
}
Forms a response that sends existing file to a browser as a download using x-sendfile.
X-Sendfile is a feature allowing a web application to redirect the request for a file to the webserver that in
turn processes the request, this way eliminating the need to perform tasks like reading the file and sending it
to the user. When dealing with a lot of files (or very big files) this can lead to a great increase in
performance as the web application is allowed to terminate earlier while the webserver is handling the request.
The request is sent to the server through a special non-standard HTTP-header. When the web server encounters the presence of such header it will discard all output and send the file specified by that header using web server internals including all optimizations like caching-headers.
As this header directive is non-standard, the name varies depending on the used web server:
- Apache: X-Sendfile
- Lighttpd v1.4: X-LIGHTTPD-send-file
- Lighttpd v1.5: X-Sendfile
- Nginx: X-Accel-Redirect
- Cherokee: X-Sendfile and X-Accel-Redirect
So for this method to work, the X-SENDFILE option/module must be enabled by the web server and a proper
xHeader must be sent.
Note
This option allows to download files that are not under web folders, and even files that are otherwise protected
(deny from all) like .htaccess.
Side effects
If this option is disabled by the web server, when this method is called, a download dialog will open, but the downloaded file will have 0 bytes.
| public \Psr\Http\Message\ResponseInterface xSendFile ( string $filePath, string|null $attachmentName = null, string $disposition = ContentDispositionHeader::ATTACHMENT, string|null $mimeType = null, string $xHeader = 'X-Sendfile' ) | ||
| $filePath | string |
Path to a file to send. |
| $attachmentName | string|null |
The file name shown to the user. If |
| $disposition | string |
Content disposition. Either {@see \Yiisoft\Http\ContentDispositionHeader::ATTACHMENT} or {@see \Yiisoft\Http\ContentDispositionHeader::INLINE}. Default is {@see \Yiisoft\Http\ContentDispositionHeader::ATTACHMENT}. |
| $mimeType | string|null |
The MIME type of the content. If not set, it will be guessed based on the file content ({@see $filePath}). |
| $xHeader | string |
The name of the |
| return | \Psr\Http\Message\ResponseInterface |
PSR-7 compatible response (@see https://www.php-fig.org/psr/psr-7/#33-psrhttpmessageresponseinterface). |
|---|---|---|
public function xSendFile(
string $filePath,
?string $attachmentName = null,
string $disposition = ContentDispositionHeader::ATTACHMENT,
?string $mimeType = null,
string $xHeader = 'X-Sendfile',
): ResponseInterface {
$this->assertDisposition($disposition);
return $this->responseFactory
->createResponse()
->withHeader($xHeader, $filePath)
->withHeader(
ContentDispositionHeader::name(),
ContentDispositionHeader::value($disposition, $attachmentName ?? basename($filePath)),
)
->withHeader(Header::CONTENT_TYPE, $mimeType ?? $this->getFileMimeType($filePath));
}
Signup or Login in order to comment.