api-platform-file-upload
Skill2 starsChanged 6 months agoMITView on GitHub
Implement file uploads in API Platform 4.3 with the canonical pattern — VichUploaderBundle (`#[Vich\\Uploadable]`, `#[Vich\\UploadableField]`), the `multipart/form-data` format enabled on the `Post` operation (`inputFormats: ['multipart' => …]`), a `MediaObject` resource exposing `contentUrl` via a custom Normalizer that resolves the URL through `Vich\\Storage\\StorageInterface`, the OpenAPI request body annotation (`Model\\RequestBody` with `multipart/form-data` schema and a binary file property), an optional custom multipart `DecoderInterface` for JSON-in-multipart payloads, linking a `MediaObject` to another resource via a regular relation + IRI, Vich-Flysystem for S3/R2 storage, ClamAV antivirus via a Processor decorator, and the test pattern using `Symfony\\Component\\HttpFoundation\\File\\UploadedFile`. Trigger on "upload an image", "MediaObject", "multipart", or "VichUploader".
In this repository52 other files it gives its agents.
- api-platform-dto-resourcesSkill
- api-platform-errorsSkill
- api-platform-filtersSkill
- api-platform-identifiersSkill
- api-platform-mcpSkill
- api-platform-mutatorsSkill
- api-platform-openapiSkill
- api-platform-paginationSkill
- api-platform-performanceSkill
- api-platform-resilienceSkill
- api-platform-resourcesSkill
- api-platform-securitySkill
- api-platform-serializationSkill
- api-platform-state-processorsSkill
- api-platform-state-providersSkill
- api-platform-testsSkill
- api-platform-upgradeSkill
- api-platform-userSkill
- api-platform-versioningSkill
- bootstrap-checkSkill
- brainstormingSkill
- config-env-parametersSkill
- controller-cleanupSkill
- cqrs-and-handlersSkill
- daily-workflowSkill
- doctrine-batch-processingSkill
- doctrine-fetch-modesSkill
- doctrine-fixtures-foundrySkill
- doctrine-migrationsSkill
- doctrine-relationsSkill
- doctrine-transactionsSkill
- effective-contextSkill
- executing-plansSkill
- functional-testsSkill
- interfaces-and-autowiringSkill
- makefile-disciplineSkill
- messenger-retry-failuresSkill
- ports-and-adaptersSkill
- quality-checksSkill
- rate-limitingSkill
- runner-selectionSkill
- strategy-patternSkill
- symfony-cacheSkill
- symfony-messengerSkill
- symfony-schedulerSkill
- symfony-votersSkill
- tdd-with-pestSkill
- tdd-with-phpunitSkill
- test-doubles-mockingSkill
- using-symfony-superpowersSkill
- value-objects-and-dtosSkill
- writing-plansSkill
The file
We split this skill into the rules it gives an agent
2 rules. About 0 are common, 1 is distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 26
Add a MediaObjectNormalizer that resolves contentUrl via Vich\Storage\StorageInterface::resolveUri().
- Distinctive: line 31
Uploads only via POST: PUT/PATCH multipart is not supported by API Platform.
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
API Platform 4.3 — File upload
Use when
- The API needs to accept file uploads (images, documents, exports re-uploaded by users).
- A resource has an image / file relation (e.g.
Book::image). - Uploads must be authenticated and validated for size / MIME type.
- Files need to live on S3 / R2 / Cloud storage instead of the local filesystem.
Default workflow
composer require vich/uploader-bundle(+vich/flysystem-bundlefor cloud storage).- Configure Vich mappings (uri_prefix, upload_destination, namer).
- Enable the
multipart/form-dataformat on thePostoperation only — never on PUT/PATCH (unsupported by API Platform for multipart). - Model a
MediaObjectresource with#[Vich\Uploadable]+#[Vich\UploadableField(fileNameProperty: 'filePath')]. - Add a
MediaObjectNormalizerthat resolvescontentUrlviaVich\Storage\StorageInterface::resolveUri(). - (Optional) Add a multipart
DecoderInterfaceif you need JSON nested in a multipart part. - Apply security:
is_granted('ROLE_USER')minimum; consider antivirus and quota voters.
Guardrails
- Uploads only via POST. PUT/PATCH multipart is not supported by API Platform.
- Validate file size + MIME with
#[Assert\File(maxSize: '5M', mimeTypes: ['image/jpeg', 'image/png'])]. - Storage path must be public (
/public/media/) or fronted by a CDN. - Antivirus for any app that accepts files from untrusted users — wire ClamAV via a Processor decorator.
- No raw entity exposure — always normalize via
contentUrlrather than the rawfilePath.
Progressive disclosure
SKILL.mdcovers posture and rules.reference.mdcarries the full pattern: prerequisites, configuration, theMediaObjectresource, theMediaObjectNormalizer(ALREADY_CALLEDpattern), optional multipart Decoder, linking to other resources, constraints, S3/R2 storage, antivirus integration, and the test recipe.
Output contract
- A
MediaObjectresource acceptingmultipart/form-dataon POST only. - A normalizer producing a
contentUrlfield (absolute, cacheable). - Validation on
file(size, MIME). - Security on the upload operation.
- Functional test using
UploadedFile+ schema assertion.
References
reference.md
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

