【问题标题】:Symfony 1.4 can't get submitted filesSymfony 1.4 无法获取提交的文件
【发布时间】:2011-04-28 10:06:04
【问题描述】:

必须是一些简单的解决方法,但作为一个初学者,我已经花了 2 天时间试图找出答案。我有一个简单的表单(“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
}

【问题讨论】:

  • 好的,我明白了我的问题,我使用的是 jq 插件,提交按钮是 jq_submit_to_remote(),这不是我应该做的。

标签: symfony1 symfony-1.4


【解决方案1】:

您应该提供您正在尝试使用的实际代码。您还可以进一步查看表单组件,该组件有一个关于文件上传的部分以及此处的示例代码(页面底部):

http://www.symfony-project.org/forms/1_2/en/02-Form-Validation

这部分文档仍然与 symfony 1.4 相关。

【讨论】:

    【解决方案2】:

    您的表单小部件配置是否如下所示?

    $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/
    

    【讨论】:

      【解决方案3】:

      您是否像这样绑定文件和表单参数?

      $this->form->bind($request->getParameter($this->form->getName()), $request->getFiles($this->form->getName()));
      

      【讨论】:

      • 如果我在保存时不使用表单对象,是否必须绑定变量?
      • 不,你可以传入一个空值:$this->form->bind(null, $request->getFiles($this->form->getName()));
      【解决方案4】:

      您必须在表单中添加 enctype="multipart/..." 属性

      【讨论】:

      • 在回答之前尝试阅读问题 ;-) 他说他的表单中有 multipart 属性。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-05
      相关资源
      最近更新 更多