loader = $twig->getLoader(); $this->entityManager = $entityManager; $this->fileService = $fileService; } #[Route('/file', name: 'file-nope')] public function index(): Response { return throw $this->createNotFoundException(); } #[Route('/file/internal', name: 'file-internal-nope')] public function indexInternal(): Response { return throw $this->createNotFoundException(); } #[Route('/file/public', name: 'file-public-nope')] public function indexPublic(): Response { return throw $this->createNotFoundException(); } #[Route('/file/internal/{filename}', name: 'file-internal')] public function accessInternalFile($filename): Response { $path = $this->fileService->getInternalDirectoryPath(); $pathinfo = pathinfo($filename); if ( isset($pathinfo['basename']) && $pathinfo['basename'] != '' && is_file($path . DIRECTORY_SEPARATOR . $pathinfo['basename']) ) { $response = new BinaryFileResponse($path . DIRECTORY_SEPARATOR . $pathinfo['basename']); // you can modify headers here, before returning return $response; } return throw $this->createNotFoundException(); } #[Route('/file/public/{filename}', name: 'file-public')] public function accessPublicFile($filename): Response { $path = $this->fileService->getPublicDirectoryPath(); $pathinfo = pathinfo($filename); if ( isset($pathinfo['basename']) && $pathinfo['basename'] != '' && is_file($path . DIRECTORY_SEPARATOR . $pathinfo['basename']) ) { $response = new BinaryFileResponse($path . DIRECTORY_SEPARATOR . $pathinfo['basename']); // you can modify headers here, before returning return $response; } return throw $this->createNotFoundException(); } #[Route('/file/stream/internal/{filename}', name: 'stream-internal')] public function streamInternalFile($filename): Response { $path = $this->fileService->getInternalDirectoryPath(); $pathinfo = pathinfo($filename); if ( isset($pathinfo['basename']) && $pathinfo['basename'] != '' && is_file($path . DIRECTORY_SEPARATOR . $pathinfo['basename']) ) { $fullpath = $path . DIRECTORY_SEPARATOR . $pathinfo['basename']; $response = new StreamedResponse(function () use ($fullpath) { $stream = fopen($fullpath, 'r'); while (!feof($stream)) { echo fread($stream, 1024); flush(); } fclose($stream); }); $response->headers->set('Content-Type', 'application/octet-stream'); $response->headers->set('Content-Disposition', 'attachment; filename="'.$filename.'"'); return $response; } return throw $this->createNotFoundException(); } }