There is a post hidden within the symfony documentation that discusses how to upload a file using the new forms framework within symfony 1.2. If you are curious about the best way to handle file uploads, it’s worth reading this What’s New in Symfony 1.2 post first, and then come back here for a quick summary of how this works.
Let’s say you would like to allow users to upload PDF files to your site. You store data related to these file uploads in an “article” table in your database. This table has a column named “file” that stores the file name of the uploaded pdf.
The symfony forms framework will generate the basic (and not so basic) code to get you started. If you haven’t yet generated your form classes yet, you can do so with the command:
./symfony propel:build-formsNow navigate to lib/form/ArticleForm.class.php. To convert the file column to a file input field, we do the following:
class ArtistResumeForm extends BaseArtistResumeForm { public function configure() { $this->widgetSchema['file'] = new sfWidgetFormInputFile(); $this->validatorSchema['file'] = new sfValidatorFile(array('path' => sfConfig::get('sf_upload_dir').'/articles', 'required' => false)); } }
Now, whenever you call $form->save(), your files will automatically be uploaded to the server, and the field “file” in your propel object will be updated with the new file name so you can easily retrieve it later.
If you are interested in getting this to work with embedded forms, check out this other post on uploading files within embedded forms with symfony 1.2.




Hi,
Is there a way to use this with merged form ?
I’m using sfGuardPlugin and I ve created a File Column in my sfGuardUserProfile but it doesn’t work.
Thank you,
Franck
By Franck on Jan 26, 2009