Skip to content

Instantly share code, notes, and snippets.

@derhansen
Created March 9, 2019 06:39
Show Gist options
  • Save derhansen/c56ff4df72d6b83121bea99bd83271cd to your computer and use it in GitHub Desktop.
Save derhansen/c56ff4df72d6b83121bea99bd83271cd to your computer and use it in GitHub Desktop.
TYPO3 - Extbase stream file from resourceStorage using streamFile PSR-7 Response
public function downloadAction()
{
$storage = $this->resourceFactory->getDefaultStorage();
$file = $storage->getFile('test.jpg');
$response = $storage->streamFile($file, true, 'test-filename.jpg');
$this->sendResponse($response);
exit();
}
protected function sendResponse($response)
{
if (!headers_sent()) {
if (http_response_code() === 200) {
header('HTTP/' . $response->getProtocolVersion() . ' ' . $response->getStatusCode() . ' ' . $response->getReasonPhrase());
}
foreach ($response->getHeaders() as $name => $values) {
header($name . ': ' . implode(', ', $values));
}
}
$body = $response->getBody();
echo $body->__toString();
}
@lgescobar
Copy link

Nice one!! I didn't know about \TYPO3\CMS\Core\Resource\ResourceStorage::streamFile().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment