This post is presuming that you …
- have Symfony 1.2+
- have models set up
- have some sort of admin area set up, albeit without file uploads
This is *not* about storing file data in the database, it is about storing file *locations* in the database. The actual file data will be stored on the filesystem.
So lets get file uploads set up. Firstly, go into the form class for the model which you want to accept file uploads (you know, /lib/form/doctrine/YourModelForm.class.php).
You need to configure a widget and a validator. In case you’re not already familiar, the widget is (sort-of) the input type (textarea, drop-down box, etc) and the validator is the thing that says “Yes, this input is valid” or “No, this input is bad”. You choose various widgets and various validators, each having their own subset of options.
In the Form class, add a configure function. Make sure it calls the parent configure function so all the base config happens!
class MyForm extends BaseMyForm { public function configure() { parent::configure(); } }
Now, add a widget to your field. Note that where it says “the_image_url”, that’s simply what I’ve called the column in my schema. You might have called your column “image” or “file” or something – so update it as necesarry.
class MyForm extends BaseMyForm { public function configure() { parent::configure(); $this->widgetSchema['the_image_url'] = new sfWidgetFormInputFileEditable(array( 'label' => 'The Image', 'file_src' => '/uploads/images/'.$this->getObject()->getTheImageUrl(), 'is_image' => true, 'edit_mode' => !$this->isNew(), 'template' => ' <div>%file%%input%%delete% %delete_label%</div> ' )); } }
Nearly done – add a validator. In my case I will only allow images, so I set ‘web_images’ as the mime_types option:
class MyForm extends BaseMyForm { public function configure() { parent::configure(); $this->widgetSchema['the_image_url'] = new sfWidgetFormInputFileEditable(array( 'label' => 'The Image', 'file_src' => '/uploads/images/'.$this->getObject()->getTheImageUrl(), 'is_image' => true, 'edit_mode' => !$this->isNew(), 'template' => ' <div>%file%%input%%delete% %delete_label%</div> ' )); $this->validatorSchema['the_image_url'] = new sfValidatorFile(array( 'required' => false, 'path' => sfConfig::get('sf_upload_dir').'/images', 'mime_types' => 'web_images', )) } }
And finally, add a ‘delete’ button so you can delete the image (optional, but if you don’t do this, you should probably remove the %delete% stuff from the template section of the file field’s widget!)
class MyForm extends BaseMyForm { public function configure() { parent::configure(); $this->widgetSchema['the_image_url'] = new sfWidgetFormInputFileEditable(array( 'label' => 'The Image', 'file_src' => '/uploads/images/'.$this->getObject()->getTheImageUrl(), 'is_image' => true, 'edit_mode' => !$this->isNew(), 'template' => ' <div>%file%%input%%delete% %delete_label%</div> ' )); $this->validatorSchema['the_image_url'] = new sfValidatorFile(array( 'required' => false, 'path' => sfConfig::get('sf_upload_dir').'/images', 'mime_types' => 'web_images', )) $this->validatorSchema['feature_image_url_delete'] = new sfValidatorPass(); } }
Then go for a “symfony cc”, generate your admin area if you haven’t already, and enjoy your lovely file uploads
Hope that helps.
08/05/2009 at 4:55 pm Permalink
Hi,
I tried to upload a file using admin generator since several weeks (or months)…
I became crazy that I never found the right way for this service.
Your script is not very good : it’s just perfect !!!
I used this script on a semi-professionnal website : Burkina Faso sur la Côte d’Azur
Thank you so much !!!
22/05/2009 at 12:36 pm Permalink
thanks
27/08/2009 at 1:53 pm Permalink
Thanks a lot for sharing this! =D
I’m a begginner using Symfony and this solution was perfect to me. These are very simple things to do…. When you know how to. =P
Keep up the good work…
11/09/2009 at 5:29 pm Permalink
Thanks for that!!
this finished 2 hours of searching for solutions
09/01/2010 at 10:40 am Permalink
Thanks for your suggestion