必须是一些简单的解决方法,但作为一个初学者,我已经花了两天时间试图找出答案。我有一个简单的表单(“enctype =”multipart / form-data“),几乎没有文本和文件的输入。我不能得到什么错,我可以通过getParameter()获取用户的文本数据,但无法获取文件getFiles()。我在ubuntu 11.04 php5.3
代码:
表单类:
public function configure()
{
$this->setWidgets(array(
'vendor' => new sfWidgetFormInputText(array(), array('class' => 'form-text')),
'image' => new sfWidgetFormInputFile(array(), array('class' => 'form-text')),
'city' => new sfWidgetFormInputText(),
'email' => new sfWidgetFormInputText(array('default' => 'simple@sample.org')),
'contacts' => new sfWidgetFormTextarea(),
'description' => new sfWidgetFormTextarea(),
));
$this->setValidators(array(
'vendor' => new sfValidatorString(array('required' => false)),
'image' => new sfValidatorFile(array(
'required' => false,
'path' => sfConfig::get('sf_upload_dir') . '/images',
'mime_types' => 'web_images')),
'city' => new sfValidatorString(array('required' => false)),
'email' => new sfValidatorString(array('required' => false)),
'contacts' => new sfValidatorString(array('required' => false)),
'description' => new sfValidatorString(array('required' => false)),
));
$this->widgetSchema->setNameFormat('pv[%s]');
}
动作:
public function executeCreate(sfWebRequest $request)
{
$this->forward404Unless($request->isMethod(sfRequest::POST));
return $this->renderText(var_dump($request->getParameter('pv'))); //this array include vars from form
//return $this->renderText(var_dump($request->getFiles('pv'))); //empty array
}
答案 0 :(得分:0)
您应该提供您正在尝试的实际代码。您还可以进一步查看表单组件,其中包含文件上传部分,其中包含示例代码(页面底部):
http://www.symfony-project.org/forms/1_2/en/02-Form-Validation
这部分文档仍与symfony 1.4相关。
答案 1 :(得分:0)
您的表单窗口小部件配置是否与以下内容类似?
$this->setWidget('attachment', new sfWidgetFormInputFileEditable(array(
'file_src' => '/upload/' . $this->getObject()->getAttachment(),
'edit_mode' => true,
'with_delete' => true,
)));
$this->setValidator('attachment', new sfValidatorFile(array(
'path' => sfConfig::get('sf_upload_dir'),
'required' => false
)));
$this->setValidator('attachment_delete', new sfValidatorPass());
另外,检查您是否对上传文件夹有权限:
chmod 777 web/upload/
答案 2 :(得分:0)
您是否绑定了文件以及表单参数,如此?
$this->form->bind($request->getParameter($this->form->getName()), $request->getFiles($this->form->getName()));
答案 3 :(得分:-2)
您必须将enctype =“multipart / ...”属性添加到表单