Problema Cform Upload

Salve a tutti,

ho il seguente problema: non riesco a far fare l’upload di una immagine in un form.

Bando alle ciance, so che per certo c’è qualcosa che sbaglio quindi vi descrivo il controller, i model e la view.

Controller:




 class addAction extends CAction{

    public function run(){

        $_module = $this->controller->getModule();

        $_form = new CForm('application.modules.administration.views.language._createForm');

        $_form['language']->model = new Language;

        $_form['uploadimage']->model = new UploadImageForm;

        $_pathToOriginal = 'images\jitr\originals';

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

            $_form['uploadimage']->model->attributes = $_POST['UploadImageForm'];

            if ($_form->submitted('register') && $_form->validate()) {

                $_img = CUploadedFile::getInstance($_form['uploadimage']->model, "uploadimage");

                $_form['uploadimage']->model->uploadimage = $_img->name;

                            if (is_null($_form['uploadimage']->model->uploadimage)){

//ho inserito questo pezzo di codice per vedere se effettivamente si carica qualcosa ma

//ogni volta che si sceglie una immagine e si fa il submit del form, inevitabilmente finisco quì

                    echo('è nullo!!');

                    exit();

                    }

                $_newLanguage = new Language();

                $_newLanguage->attributes = $_form['language']->attributes;                

            }

        }

        $this->controller->render('create',array('module'=>$_module,'form' => $_form));

    }

}



il modello UploadImageForm




class UploadImageForm extends CFormModel {


    public $uploadimage;

    private $_maxSize;


    public function rules() {

        $this->_maxSize = 1024 * 200;

        return array(

            array('uploadimage', 'required'),

            array(

                'uploadimage', 'file','allowEmpty'=>'true', 'types' => 'jpg, gif, png', 'maxSize' => $this->_maxSize,

                'tooLarge' => Yii::t('pageBody', 'File too big, Max size allowed: ' . (int) ($this->_maxSize / 1024) . 'kb.'),

            ),

        );

    }


    public function attributeLabels() {

        return array(

            'uploadimage' => Yii::t('pageBody', 'Image'),

        );

    }


}



la vista create




<?php

$this->pageTitle = Yii::t('pageBody', 'Languages Administration') . " - " . Yii::app()->name;

$this->breadcrumbs = array(

    Yii::t('pageBody', 'Languages Administration') => "/" . $module->name . "/language/",

    Yii::t('pageBody', 'Add new Language')

);

?>

<br />

<div class="span-19">

    <div class="form">

        <?php echo($form->render());?>

    </div>

</div>



ed il form _createForm è così fatto




<?php


return array(

    'elements' => array(

        'language' => array(

            'showErrorSummary' => true,

            'type' => 'form',

            'title' => Yii::t('pageBody', 'Language Information'),

            'elements' => array(

                'name' => array(

                    'type' => 'text',

                    'value'=> ''

                ),

                'code' => array(

                    'type' => 'text',

                ),

                'image' => array(

                    'type' => 'hidden',

                    

                ),

                'status' => array(

                    'type' => 'dropdownlist',

                    'items' => Language::model()->GetStatus(),

                    'prompt' => Yii::t('pageBody', 'Please select:'),

                ),

                'created_at' => array(

                    'type' => 'hidden',

                    'value' => date("Y-m-d H:i:s")

                ),

                'updated_at' => array(

                    'type' => 'hidden',

                    'value' => date("Y-m-d H:i:s")

                ),

            ),

        ),

        'uploadimage' => array(

            'type' => 'form',

            'showErrorSummary' => true,

            'title' => Yii::t('pageBody', 'Flag Image Upload'),

            'attributes' => array(

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

            ),

            'elements' => array(

                'uploadimage' => array(

                    'type' => 'file',

                ),

            )

        ),

    ),

    'buttons' => array(

        'register' => array(

            'type' => 'submit',

            'label' => Yii::t('pageBody', 'Save'),

        ),

    ),

);

?>



come descritto nel commento all’interno dell’action, il form viene creato in modo corretto e la validazione effettuata secondo le regole, insomma tutto sembra a posto ma quando scelgo una immagine, la carico e faccio submit, il risultato delle due righe di codice:




                $_img = CUploadedFile::getInstance($_form['uploadimage']->model, "uploadimage");

                $_form['uploadimage']->model->uploadimage = $_img->name;



è sempre nullo, come se non avessi caricato niente.

Ci sto perdendo la testa da un paio di giorni, e la cosa che più mi manda in bestia è che sono certo sia una sciocchezza che non riesco a vedere.

Vi prego datemi una mano.

Vedo che cerchi lìimmagine in $_POST[‘UploadImageForm’]. Ma quando fai un upload, i files si trovano nell’array super globale $_FILES.

because i cant understand italian language, if i can help you is passing you a very usefull link:

http://github.com/valums/file-uploader

this file uploader tool will perform all the dirty work in a very beauty and clean way. please refer to the product documentation, if it dont help, i can help creating for you a view and controller that use this upload tool.

Grazie per le risposte, appena possibile proverò a rivedere quanto scritto alla luce dei vostri suggerimenti.

Thanks for your reply, as soon as possible I’ll try again following your suggestions and I’ll let you know.

Hi I took a look at the code and at the documentation but I’m not sure to have correctly understood how I should use this tool so…yes I would really appreciate if you could show me an example.

Thanks a lot in advance

Ho Risolto!

SOLVED!

il problema era quì:

Problem was here!




<?php


return array(

    'elements' => array(

        'language' => array(

...

...

)

        'uploadimage' => array(

            'type' => 'form',

            'showErrorSummary' => true,

            'title' => Yii::t('pageBody', 'Flag Image Upload'),

            'attributes' => array(

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

            ),

            'elements' => array(

                'uploadimage' => array(

                    'type' => 'file',

                ),

            )

        ),

    ),

    'buttons' => array(

        'register' => array(

            'type' => 'submit',

            'label' => Yii::t('pageBody', 'Save'),

        ),

    ),

);

?>

scrivere il form in questo modo fa sì che l’attributo ‘enctype’ => ‘multipart/form-data’ sia dato ad un fieldset! anziché al form!

Writing the Form in this way lead the enctype attribute to be applied to a Fieldset tag instead that to a Form.

avrei dovuto invece scrivere il tutto in questo modo:

That’s how it should be written instead.




<?php


return array(

    'attributes' => array(

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

    ),

    'elements' => array(

        'language' => array(

            'showErrorSummary' => true,

            'type' => 'form',

            'title' => Yii::t('pageBody', 'Language Information'),

            'elements' => array(

                'name' => array(

                    'type' => 'text',

                    'value' => ''

                ),

                'code' => array(

                    'type' => 'text',

                ),

                'image' => array(

                    'type' => 'hidden',

                ),

                'status' => array(

                    'type' => 'dropdownlist',

                    'items' => Language::model()->GetStatus(),

                    'prompt' => Yii::t('pageBody', 'Please select:'),

                ),

                'created_at' => array(

                    'type' => 'hidden',

                    'value' => date("Y-m-d H:i:s")

                ),

                'updated_at' => array(

                    'type' => 'hidden',

                    'value' => date("Y-m-d H:i:s")

                ),

            ),

        ),

        'uploadimage' => array(

            'type' => 'form',

            'showErrorSummary' => true,

            'title' => Yii::t('pageBody', 'Flag Image Upload'),

            'elements' => array(

                'uploadimage' => array(

                    'type' => 'file',

                ),

            )

        ),

    ),

    'buttons' => array(

        'register' => array(

            'type' => 'submit',

            'label' => Yii::t('pageBody', 'Save'),

        ),

    ),

);

?>



Thanks a lot

ok, maybe and if your case is not urgent, then i should write an entire example and publish it on yii forum in order to get accesible by others yii users. As I said, is a very powerfull and "easy way" to manage uploading, the tool supports multiupload, progress bar per upload, some basic error control on uploaded file, and is very easy to implement, but a few components are required to make it run.

i promise you the wiki…here is:

http://www.yiiframework.com/wiki/326/nice-file-upload-tool-on-yii-framework/

Thanks a lot! Really interesting. I’ll give it a try for sure.