SUBSCRIBE VIA RSS


Subscribe to our feed

Symfony Experts

Symfony Experts
If you have an urgent question for a symfony-related issue, this is the place to ask.

Topics

Stack Overflow


The old fashioned way

RECENT TUNES

January 23, 2009 – 4:42pm Uploading a file with Symfony 1.2

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-forms

Now 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.

Posted by in  Web Development   |  

2 Responses to Uploading a file with Symfony 1.2

  1. Franck says:

    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

  2. Jon Labrador says:

    Hi,

    a need to upload file using i18n. I have tested thousands of times unsuccessfully

    can you help me!
    thanks