symfonycontrib/filefield-bundle
SymfonyContrib FileFieldBundle provides a reusable file upload form field for Symfony applications, simplifying file selection, validation, storage, and form integration while keeping upload handling consistent and easy to configure.
symfonycontrib/filefield-bundle is a Symfony 2.x-era bundle that enhances the core file form type. Its value-add is exactly the thing Symfony's native FileType never did out of the box: render a link to the currently stored file, expose a delete checkbox, and reconcile "keep / replace / remove" on submit. If you are on Symfony 3.4+ you almost certainly want VichUploaderBundle instead — but on a legacy 2.x codebase this bundle solves a real, recurring annoyance.composer.json (there is no maintained stable release line; expect a dev-master constraint) and run composer update.app/AppKernel.php: new SymfonyContrib\FileFieldBundle\FileFieldBundle(),. Do this for the environments that build forms (all of them, realistically).Resources/config/ for the service/form-type alias, and the Form/Type/ directory for the actual type class and its setDefaultOptions/configureOptions — that is your source of truth for option names (upload_dir, multiple, size/mime limits, delete option, etc.). The README is thin; the type class is the documentation.photo column holding a stored path/string. Build the form with the bundle's file type instead of the core file type, point upload_dir at your web-accessible uploads directory, and the widget will render: current file link + [x] delete + file input. Handle the file in your controller/entity exactly as you would a normal upload.PRE_SUBMIT/SUBMIT your handler must branch three ways: (1) new UploadedFile present → move it, delete the old one, persist the new path; (2) delete checkbox checked and no new file → delete the old file and null the column; (3) neither → leave everything untouched. Write this as a private handleFile() method on the controller or an entity listener so every form reuses one implementation.prePersist/preUpdate (or @PrePersist) on the owning entity lets you call the same move/delete logic regardless of whether the entity was saved via a form, a fixture, or an import. Keep the stored value as a relative path and resolve the absolute path from a single upload_dir parameter — never store absolute paths in the DB.multiple option (a file-collection widget). Persist them as a FileFieldCollection/array on the entity via a OneToMany join entity with its own path + original name, and hydrate the widget's data from that collection so the rendered "current files" list is correct on every edit.uniqid/sha1 + extension) to avoid collisions and path traversal.File constraint (maxSize, mimeTypes, extensions) on the entity/form as the authoritative gate — options on the widget are display-time conveniences and are easy to bypass.{{ form_row(form.photo) }} and let it render link + checkbox + input. If you need custom markup, access form.photo.vars.data for the current path and the delete field by its generated child name.file alias globally (check the service alias before assuming), every existing ->add('x', 'file') in the app now renders a link + checkbox — verify your other forms still behave before shipping.VichUploaderBundle + Symfony\Component\HttpFoundation\File\UploadedFile is the sane replacement.file type, it silently changes existing forms. Check Resources/config/*.xml|yml for the form.type alias and whether it is file or a distinct name like filefield; the safest adoption is to use the distinct name explicitly.$form->getData() may hand you a string path (existing file kept), an UploadedFile (new upload), or null (no data). Always type-check before calling ->move() or string functions, or you will fatal on the first edit-after-upload.upload_dir must exist and be writable by the web user; the bundle does not create it. Set upload_tmp_dir, post_max_size, upload_max_filesize, and max_file_uploads in php.ini — the bundle's size option runs after PHP has already rejected oversized bodies, so mismatched limits produce confusing silent failures.../, unicode, NUL bytes). Generate a server-side name and keep the original name only as display metadata.upload_dir for anything sensitive — route downloads through a controller with BinaryFileResponse and an authorization check. The rendered "current file" link is only as safe as the path you stored.{{ dump(form.photo.vars) }} (Twig debug bundle) is the fastest way to see what data the widget actually received; most "it shows the wrong file / no link" bugs are a wrong mapped data value, not a bundle bug.buildView/configureOptions and the service definitions are minimal — if you need different markup, override the form theme block in the bundle's Resources/views/Form/ rather than editing vendor code.How can I help you explore Laravel packages today?