0

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

9
  • if you are new to symfony why not using symfony2 ? there is much more support for it Commented Apr 3, 2013 at 19:47
  • requeriments of the project :/ I can't choose on it Commented Apr 3, 2013 at 19:51
  • are the requirements use symfony1.4 ? Commented Apr 3, 2013 at 19:54
  • 2
    First: you should use <?php echo $form->renderHiddenFields() ?> to render _csrf_token field (and other hidden input). Second: you should use path config value inside the sfValidatorFile to handle the move_uploaded_file. Go there and check the part about file upload (search for sfWidgetFormInputFile). Commented Apr 4, 2013 at 7:52
  • 1
    @Gonzalo.- wop sorry didn't see your comment :) Commented Apr 5, 2013 at 13:30

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.