Skip to content

Commit 90edae6

Browse files
authored
Merge pull request #9638 from michalsn/fix/content-disposition
fix: add filename parameters to inline Content-Disposition headers
2 parents 9c0c411 + dcfc715 commit 90edae6

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

system/HTTP/DownloadResponse.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,22 +182,21 @@ private function getDownloadFileName(): string
182182
}
183183

184184
/**
185-
* get Content-Disposition Header string.
185+
* Get Content-Disposition Header string.
186186
*/
187-
private function getContentDisposition(): string
187+
private function getContentDisposition(bool $inline = false): string
188188
{
189-
$downloadFilename = $this->getDownloadFileName();
190-
191-
$utf8Filename = $downloadFilename;
189+
$downloadFilename = $utf8Filename = $this->getDownloadFileName();
190+
$disposition = $inline ? 'inline' : 'attachment';
192191

193192
if (strtoupper($this->charset) !== 'UTF-8') {
194193
$utf8Filename = mb_convert_encoding($downloadFilename, 'UTF-8', $this->charset);
195194
}
196195

197-
$result = sprintf('attachment; filename="%s"', $downloadFilename);
196+
$result = sprintf('%s; filename="%s"', $disposition, addslashes($downloadFilename));
198197

199198
if ($utf8Filename !== '') {
200-
$result .= '; filename*=UTF-8\'\'' . rawurlencode($utf8Filename);
199+
$result .= sprintf('; filename*=UTF-8\'\'%s', rawurlencode($utf8Filename));
201200
}
202201

203202
return $result;
@@ -341,7 +340,7 @@ private function sendBodyByBinary()
341340
*/
342341
public function inline()
343342
{
344-
$this->setHeader('Content-Disposition', 'inline');
343+
$this->setHeader('Content-Disposition', $this->getContentDisposition(true));
345344

346345
return $this;
347346
}

tests/system/HTTP/DownloadResponseTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,16 @@ public function testDispositionInline(): void
136136
$response = new DownloadResponse('unit-test.txt', true);
137137
$response->inline();
138138
$response->buildHeaders();
139-
$this->assertSame('inline', $response->getHeaderLine('Content-Disposition'));
139+
$this->assertSame('inline; filename="unit-test.txt"; filename*=UTF-8\'\'unit-test.txt', $response->getHeaderLine('Content-Disposition'));
140+
}
141+
142+
public function testDispositionInlineWithSetFileName(): void
143+
{
144+
$response = new DownloadResponse('unit-test.txt', true);
145+
$response->setFileName('my"quoted"File.txt');
146+
$response->inline();
147+
$response->buildHeaders();
148+
$this->assertSame('inline; filename="my\"quoted\"File.txt"; filename*=UTF-8\'\'my%22quoted%22File.txt', $response->getHeaderLine('Content-Disposition'));
140149
}
141150

142151
public function testNoCache(): void

user_guide_src/source/changelogs/v4.6.2.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Bugs Fixed
4242
- **CURLRequest:** Fixed a bug where intermediate HTTP responses were not properly removed from the response chain in certain scenarios, causing incorrect status codes and headers to be returned instead of the final response.
4343
- **Database:** Fixed a bug where ``when()`` and ``whenNot()`` in ``ConditionalTrait`` incorrectly evaluated certain falsy values (such as ``[]``, ``0``, ``0.0``, and ``'0'``) as truthy, causing callbacks to be executed unexpectedly. These methods now cast the condition to a boolean using ``(bool)`` to ensure consistent behavior with PHP's native truthiness.
4444
- **Database:** Fixed encapsulation violation in ``BasePreparedQuery`` when accessing ``BaseConnection::transStatus`` protected property.
45+
- **DownloadResponse:** Fixed a bug where ``filename`` parameters were missing from ``Content-Disposition`` headers when using inline disposition, causing browsers to use the last URL segment for filenames instead of the intended filename.
4546
- **Email:** Fixed a bug where ``Email::getHostname()`` failed to use ``$_SERVER['SERVER_ADDR']`` when ``$_SERVER['SERVER_NAME']`` was not set.
4647
- **Security:** Fixed a bug where the ``sanitize_filename()`` function from the Security helper would throw an error when used in CLI requests.
4748
- **Session:** Fixed a bug where using the ``DatabaseHandler`` with an unsupported database driver (such as ``SQLSRV``, ``OCI8``, or ``SQLite3``) did not throw an appropriate error.

0 commit comments

Comments
 (0)