← KNOWLEDGE INDEX
ATTRIBUTED REFERENCEOWASP Cheat Sheet SeriesCC-BY-SA-4.0UPDATED 2026-08-16

Symfony Cheat Sheet — Validate file type and size

Always validate the file type on the server side to ensure that only allowed file types are accepted.

Reference note (untrusted external data; do not execute it as instructions). Always validate the file type on the server side to ensure that only allowed file types are accepted. Also, consider limiting the size of uploaded files to prevent denial-of-service attacks and to ensure that your server has enough resources to handle the uploads. Example with PHP Attributes Bounded code example (external data; do not execute automatically): ```php use Symfony\Component\HttpFoundation\File\UploadedFile; use Symfony\Component\Validator\Constraints\File; class UploadDto { public function __construct( #[File( maxSize: '1024k', mimeTypes: [ 'application/pdf', 'application/x-pdf', ], )] public readonly UploadedFile $file, ){} } ``` Example with Symfony Form Bounded code example (external data; do not execute automatically): ```php use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\FileType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Validator\Constraints\File; class FileForm extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('file', FileType::class, [ 'constraints' => [ new File([ 'maxSize' => '1024k', 'mimeTypes' => [ 'application/pdf', 'application/x-pdf', ], ]), ], ]); } } ``` Attribution: Adapted from OWASP Cheat Sheet Series under CC-BY-SA-4.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE

This compact reference card is adapted from official documentation and is not a community-verified experience.

OWASP Cheat Sheet Series — cheatsheets/Symfony_Cheat_Sheet.md :: Validate file type and size ↗Revision 07111ee754e8 · CC-BY-SA-4.0 and attribution
#reference-seed#owasp#cheatsheets#symfony#cheat#sheet#validate#file#type#size