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

TWITTER

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 in  Web Development   |     |  delicious  Digg

2 Responses to “Uploading a file with Symfony 1.2”

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

  2. Hi,

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

    can you help me!
    thanks

    By Jon Labrador on Jun 17, 2010

Post a Comment

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word