doubijiao2094 2013-04-03 19:37
浏览 42
已采纳

使用symfony上传和文件验证

I'm pretty new in Symfony, and I'm having a problem uploading a file

I just don't get it work, and I can't find something that tell me exactly how to use it or show me how it works (I had read smyfony official documentation)

this is my code:

the form

class PagosForm extends BasePagosForm
{
  public function configure()
  {
    $this->setWidgets(array('file' => new sfWidgetFormInputFile()));

    $this->widgetSchema->setNameFormat('pagos[%s]');

    $this->setValidators(array('file' => new sfValidatorFile(array(
        'required'   => false
        ))));
  }
}

the actions

public function executeSelect(sfWebRequest $request)
  {
     $this->form = new PagosForm();
  }

 public function executeUpload(sfWebRequest $request)
  {

    $this->form = new PagosForm();

    if ($request->isMethod('post'))
    {
      $this->form->bind($request->getParameter($this->form->getName()), $request->getFiles($this->form->getName()));

      if ($this->form->isValid())
      {
        echo "isvalid";
        move_uploaded_file($_FILES["file"]["tmp_name"], sfConfig::get('sf_upload_dir').'/'.$_FILES["file"]["name"]);
      }
      else 
      {  
        echo "isinvalid";
        //echo var_dump($this->form);
      }
    }
  }

in select, there is outputed a button for selecting file and submit it: that takes us to the action Upload.

selectSuccess.php

<?php echo form_tag('pagos/upload', 'enctype="multipart/form-data"', 'method="POST"') ?>
<?php echo $form['file'] ?>
<?php echo submit_tag('Send') ?>

I have some problems with eclipse, so I'm using Sublime text 2 (a text's editor) for coding, so its difficult for me find the errors.

I use move_uploaded_file because for some reason, I can't make it work the functions of symfony for uploading files. In this way, I can get it, but if I want to add some validation (for example, only allow to upload excel files), I can't get it work..

I always get line "isinvalid" printed, except I remove the line in the form that set validator. So, for some reason, the validator is not working..

I've tried to create my custom validator also, extending sfValidatorFile, but not working at all (I can't even upload any file or render the page)

So, I need a little help, before my head explodes (caused by symfony :P)

what is wrong with the code, that I always getting invalid file ? I'm trying to uploading a simple .txt of 3 KB, if I remove the valitadion, I can upload it normally

  • 写回答

1条回答 默认 最新

  • dream0614 2013-04-05 13:06
    关注

    As no one answer this, I will put it myself:

    I needed to use <?php echo $form->renderHiddenFields() ?> to render _csrf_token field (and other hidden input).

    with this, the validation has passed

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码