8000 [HttpFoundation] Added a switch to delete file after the response is send by WybrenKoelmans · Pull Request #11212 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[HttpFoundation] Added a switch to delete file after the response is send #11212

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

Merged
merged 1 commit into from
Jul 9, 2014
Merged
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
20 changes: 20 additions & 0 deletions src/Symfony/Component/HttpFoundation/BinaryFileResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class BinaryFileResponse extends Response
protected $file;
protected $offset;
protected $maxlen;
protected $deleteFileAfterSend = false;

/**
* Constructor.
Expand Down Expand Up @@ -258,6 +259,10 @@ public function sendContent()

fclose($out);
fclose($file);

if ($this->deleteFileAfterSend) {
unlink($this->file->getPathname());
}
}

/**
Expand Down Expand Up @@ -289,4 +294,19 @@ public static function trustXSendfileTypeHeader()
{
self::$trustXSendfileTypeHeader = true;
}

/**
* If this is set to true, the file will be unlinked after the request is send
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there should be a mention that this will not delete the file when relying on X-Sendfile

* Note: If the X-Sendfile header is used, the deleteFileAfterSend setting will not be used.
* @param bool $shouldDelete
*
* @return BinaryFileResponse
*/
public function deleteFileAfterSend($shouldDelete)
{
$this->deleteFileAfterSend = $shouldDelete;

return $this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,24 @@ public function testXAccelMapping($realpath, $mapping, $virtual)
$this->assertEquals($virtual, $response->headers->get('X-Accel-Redirect'));
}

public function testDeleteFileAfterSend()
{
$request = Request::create('/');

$path = __DIR__.'/File/Fixtures/to_delete';
touch($path);
$realPath = realpath($path);
$this->assertFileExists($realPath);

$response = new BinaryFileResponse($realPath);
$response->deleteFileAfterSend(true);

$response->prepare($request);
$response->sendContent();

$this->assertFileNotExists($path);
}

public function getSampleXAccelMappings()
{
return array(
Expand All @@ -215,4 +233,12 @@ protected function provideResponse()
{
return new BinaryFileResponse(__DIR__ . '/../README.md');
}

public static function tearDownAfterClass()
{
$path = __DIR__.'/../Fixtures/to_delete';
if (file_exists($path)) {
@unlink($path);
}
}
}
0