Sgn.
Sgn.

Reputation: 1696

Symfony 1.4, storing an uploaded file size and name

I use sfWidgetFormInputFileEditable in my form for file handling purpose. Here is the code:

    /* File */
$this->widgetSchema['file_addr'] = new sfWidgetFormInputFileEditable(array(
    'label' => 'File',
    'file_src' => sfConfig::get('sf_root_dir').'...',
    'is_image' => false,
    'edit_mode' => !$this->getObject()->isNew(),
    'delete_label' => 'Delete?',
    'template' => '...',
    'with_delete' => true
));
$this->validatorSchema['file_addr'] = new sfValidatorFile(array(
    'required' => $this->getObject()->isNew(),
    'path' => sfConfig::get('sf_root_dir') .'/files'
));
$this->validatorSchema['file_addr_delete'] = new deleteFileValidator(...);
/* File: END */

This way it stores generated file name in file_addr field. I want to access other file data like size, file name etc and store them in database too. How can I do that?

Upvotes: 0

Views: 1771

Answers (2)

Sgn.
Sgn.

Reputation: 1696

Additional uploaded file data is available in form processValues method. So I override it like this:

  public function processValues($values)
  {
    /* File data */
    if ($values['file_addr'] instanceof sfValidatedFile)
    {
       $file = $values['file_addr'];
       $this->getObject()->setFileSize($file->getSize());
       $this->getObject()->setFileType($file->getType());
       $this->getObject()->setFileHash(md5_file($file->getTempName()));
    }
    return parent::processValues($values);
  }

Upvotes: 2

Charles
Charles

Reputation: 11778

You can override the save() method.

public function save($con = null) {
  $values = $this->getValues();
  $fileAddr = $values['file_addr'];

  // ... Do stuff with the values ...

  return parent::save($con);
}

I didn't test it, but something like that should work.

Upvotes: 2

Related Questions