[英]Symfony 1.4, storing an uploaded file size and name
我在表单中使用sfWidgetFormInputFileEditable
进行文件处理。 这是代码:
/* 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 */
这样,它将生成的文件名存储在file_addr
字段中。 我想访问其他文件数据,例如大小,文件名等,并将它们也存储在数据库中。 我怎样才能做到这一点?
您可以覆盖save()方法。
public function save($con = null) {
$values = $this->getValues();
$fileAddr = $values['file_addr'];
// ... Do stuff with the values ...
return parent::save($con);
}
我没有测试它,但是类似的东西应该起作用。
其他上传的文件数据可以通过processValues
方法获得。 所以我这样覆盖它:
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.