Source of file AttachmentResponse.php
Size: 1,689 Bytes - Last Modified: 2017-11-08T13:54:24+00:00
/home/travis/build/bluzphp/framework/src/Response/AttachmentResponse.php
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 | <?php /** * Bluz Framework Component * * @copyright Bluz PHP Team * @link https://github.com/bluzphp/framework */ declare(strict_types=1); namespace Bluz\Response; use Zend\Diactoros\Response as DiactorsResponse; use Zend\Diactoros\Response\InjectContentTypeTrait; use Zend\Diactoros\Stream; /** * Class AttachmentResponse * * @package Bluz\Response * @link http://www.marco-bunge.com/2016/09/01/file-downloads-with-zend-diactoros/ */ class AttachmentResponse extends DiactorsResponse { use InjectContentTypeTrait; /** * Create a file attachment response. * * Produces a text response with a Content-Type of given file mime type and a default * status of 200. * * @param string $file Valid file path * @param int $status Integer status code for the response; 200 by default. * @param array $headers Array of headers to use at initialization. * * @internal param StreamInterface|string $text String or stream for the message body. * @throws \InvalidArgumentException */ public function __construct($file, $status = 200, array $headers = []) { $fileInfo = new \SplFileInfo($file); $headers = array_replace( $headers, [ 'content-length' => $fileInfo->getSize(), 'content-disposition' => sprintf('attachment; filename=%s', $fileInfo->getFilename()), ] ); parent::__construct( new Stream($fileInfo->getRealPath(), 'r'), $status, $this->injectContentType((new \finfo(FILEINFO_MIME_TYPE))->file($fileInfo->getRealPath()), $headers) ); } } |