File: /var/www/vhosts/creativefellows.nl/marie.creativefellows.nl/src/Action/Download/DownloadAction.php
<?php
namespace App\Action\Download;
use App\Action\Action;
use App\Domain\Navigation\Service\NavigationReader;
use App\Domain\Page\Service\PageReader;
use App\Handler\DefaultErrorHandler;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Container\ContainerInterface;
use Slim\Psr7\Stream;
final class DownloadAction extends Action{
protected $container;
protected $pageReader;
protected $errorHandler;
public function __construct(
ContainerInterface $container,
PageReader $pageReader,
NavigationReader $navigationReader,
DefaultErrorHandler $errorHandler
){
$this->container = $container;
$this->pageReader = $pageReader;
$this->navigationReader = $navigationReader;
$this->errorHandler = $errorHandler;
$this->root_dir = $this->container->get('settings')['upload_dir'];
}
protected function action(): ResponseInterface
{
$file = $this->request->getQueryParams()['file'] ?? '';
$fh = fopen($this->root_dir . $file, 'rb');
if(!$fh){
return $this->response->withStatus(404);
}
$file_stream = new Stream($fh);
//die(basename($files[0]->url()));
// return response
return $this->response
->withBody($file_stream)
->withHeader('Cache-Control', 'must-revalidate')
->withHeader('Content-Description','File Transfer')
->withHeader('Content-Type','application/octet-stream')
->withHeader('Pragma', 'public')
->withHeader('Expires', '0')
->withHeader('Last-Modified', gmdate('D, d M Y H:i:s').' GMT')
->withHeader('Content-Disposition', 'attachment; filename=' . $file);
}
}