Cuploadedfile::getinstance Always Return Null

Hi,

I’m am using CUploadedFile::getInstance to upload a image, but it always return null. here’s my code:

On controller:




if (isset($_POST['Mestre'])) {

            $model->attributes = $_POST['Mestre'];

            $file = CUploadedFile::getInstance($model, 'fotografia');


            if ($file === null) {

                echo '<script> console.log("print_r($_FILES) = ' . print_r($_FILES) . '")</script>'; //what is printed here is a '1' on the console and an empty array on the screen..

            }



On view:




<?php

    $form = $this->beginWidget('CActiveForm', array(

        'id' => 'mestre-form',

        'enableClientValidation' => true,

        'enableAjaxValidation' => false,

        'htmlOptions' => array(

            'enctype' => 'multipart/form-data',

        ),

            ));

    ?>


........


  <div class="row">

        <?php echo $form->labelEx($model, 'fotografia'); ?>

        <?php echo $form->fileField($model, 'fotografia'); ?>

        <?php echo $form->error($model, 'fotografia'); ?>

    </div>



On Model:

I added a variable $fotografia, and add the folowing rule:




array('fotografia', 'file', 'types' => 'jpg, gif, png', 'allowEmpty' => true, 'on'=>'update'),



I previously could upload the foto, but, I don’t know why (in my point of view, I’ve not made any significant change, jus the form is now loaded by ajax request) it’s not working.

What I’m doing wrong?

Please help me. I’m struggling with that for the past two days…

You can’t send files over ajax, at least not without some trickery involving hidden iframes.

If you’re still posting the form normally with a full page request and it’s not working, make sure that the image you’re uploading conforms to your model rules and isn’t bigger than PHP’s upload limit.

Thanks Keith,

Do you know where can I find that ‘trickery’ to send files over ajax? I really would like to do it this way…

At least an explanation why this can’t work…

Thanks again!

It doesn’t work because it’s simply not supported - I guess for security reasons.

This is the sort of thing you need to do:

http://www.openjs.com/articles/ajax/ajax_file_upload/

As you have seen in another post, I’ve changed my approach. Big thanks, Kieth…