Skip to content
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

Add rename option for Response download() #2116

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions system/HTTP/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -1038,10 +1038,11 @@ protected function sendCookies()
* @param string $filename The path to the file to send
* @param string|null $data The data to be downloaded
* @param boolean $setMime Whether to try and send the actual MIME type
* @param string|null $rename An optional new name for the file when using a path
*
* @return \CodeIgniter\HTTP\DownloadResponse|null
*/
public function download(string $filename = '', $data = '', bool $setMime = false)
public function download(string $filename = '', $data = '', bool $setMime = false, string $rename = null)
{
if ($filename === '' || $data === '')
{
Expand All @@ -1056,7 +1057,7 @@ public function download(string $filename = '', $data = '', bool $setMime = fals
$filename = end($filename);
}

$response = new DownloadResponse($filename, $setMime);
$response = new DownloadResponse($rename ?? $filename, $setMime);

if ($filepath !== '')
{
Expand All @@ -1069,5 +1070,4 @@ public function download(string $filename = '', $data = '', bool $setMime = fals

return $response;
}

}
19 changes: 19 additions & 0 deletions tests/system/HTTP/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,25 @@ public function testGetDownloadResponseByFilePath()
$this->assertSame(file_get_contents(__FILE__), $actual_output);
}

public function testGetDownloadResponseRename()
{
$response = new Response(new App());
$rename = 'myFile.' . pathinfo(__FILE__, PATHINFO_EXTENSION);

$actual = $response->download(__FILE__, null, false, $rename);

$this->assertInstanceOf(DownloadResponse::class, $actual);
$actual->buildHeaders();
$this->assertSame('attachment; filename="' . $rename . '"; filename*=UTF-8\'\'' . $rename, $actual->getHeaderLine('Content-Disposition'));

ob_start();
$actual->sendBody();
$actual_output = ob_get_contents();
ob_end_clean();

$this->assertSame(file_get_contents(__FILE__), $actual_output);
}

public function testVagueDownload()
{
$response = new Response(new App());
Expand Down
3 changes: 2 additions & 1 deletion user_guide_src/source/outgoing/response.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ The first parameter is the **name you want the downloaded file to be named**, th
file data.

If you set the second parameter to NULL and ``$filename`` is an existing, readable
file path, then its content will be read instead.
file path, then its content will be read instead with an option to change the name
of the downloaded file with the fourth parameter.

If you set the third parameter to boolean TRUE, then the actual file MIME type
(based on the filename extension) will be sent, so that if your browser has a
Expand Down