requestStack->getCurrentRequest(); $file = $request?->files->get('file'); if (null === $file) { throw new BadRequestHttpException('No file uploaded.'); } if ('application/pdf' !== $file->getMimeType()) { throw new BadRequestHttpException('Only PDF files are accepted.'); } $month = $data->getMonth(); $year = $month?->format('Y') ?? date('Y'); $monthNumber = $month?->format('m') ?? date('m'); $relativePath = sprintf('mileage-receipts/%s/%s', $year, $monthNumber); $absoluteDir = sprintf('%s/%s', $this->uploadDir, $relativePath); if (!is_dir($absoluteDir)) { mkdir($absoluteDir, 0o755, true); } $filename = Uuid::v4()->toRfc4122().'.pdf'; $fullRelative = sprintf('%s/%s', $relativePath, $filename); $originalName = $file->getClientOriginalName(); $file->move($absoluteDir, $filename); $data->setReceiptPath($fullRelative); $data->setReceiptName($originalName); $this->entityManager->flush(); return new JsonResponse(['path' => $fullRelative, 'name' => $originalName], Response::HTTP_OK); } }