getClientMimeType(), $allowedTypes)) { throw new \Exception('Invalid file type.'); } // Generate a unique filename $originalName = $file->getName(); $ext = pathinfo($originalName, PATHINFO_EXTENSION); $baseName = pathinfo($originalName, PATHINFO_FILENAME); $counter = 0; $newName = $baseName . '.' . $ext; while (file_exists($uploadPath . $newName)) { $counter++; $newName = $baseName . '_' . $counter . '.' . $ext; } // Move the file to the upload directory try { $file->move($uploadPath, $newName); } catch (\Exception $e) { throw new \Exception('Failed to upload file: ' . $e->getMessage()); } return $uploadDirectory . $newName; } }