HEX
Server: Apache
System: Linux v38079.2is.nl 3.10.0-1160.119.1.el7.x86_64 #1 SMP Tue Jun 4 14:43:51 UTC 2024 x86_64
User: democfellows (10015)
PHP: 8.1.34
Disabled: opcache_get_status
Upload Files
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);
		
	}

}