File Upload

Hi!

I try to upload a file on my web-server. Here is my form:


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

            'id'=>'t-generate-licence-form',

            'enableAjaxValidation'=>true,

            'htmlOptions' => array('enctype' => 'multipart/form-data'),

    )); ?>


            <div class="row">

                <?php echo CHtml::beginForm(); ?>

                <?php echo CHtml::label('Insert licence file','file'); ?>

                <?php echo CHtml::fileField('file',''); ?>

            </div>


            <div class="row buttons">

                <?php echo CHtml::submitButton('Generate', array('name'=>'generateLicence')); ?>

            </div>

            <?php echo CHtml::endForm(); ?>


    <?php $this->endWidget(); ?>

Now my controller:


if (isset($_POST['generateLicence']))

            {

                if (isset($_FILES['file']['name']))

                {

                    print_r2($_FILES);

                    $model = new t_licence;

                    $model->licenceFile=CUploadedFile::getInstance($model,'licenceFile');

                    $model->licenceFile->saveAs('test');

                    die();

                }

            }

I have the following error: Call to a member function saveAs() on a non-object.

Here is my variable $FILES:


Array

(

    [file] => Array

        (

            [name] => licence_test.txt

            [type] => application/octet-stream

            [tmp_name] => C:\Program Files\webserver\Bin\php\Uploads\php19.tmp

            [error] => 0

            [size] => 887

        )


)

When I look in folder C:\Program Files\webserver\Bin\php\Uploads I can’t find my file. What is the problem?

Thanks by adavance.

First you open to forms in your view, one via CActiveForm widget and one via CHtml::beginForm(). Remove the latter.

Then you don’t have a call to save() in your Controller so maybe this causes also the file not to be saved by yii.

Look at the How to upload a file using a model wiki for a working example.

Thanks.

My model isn’t completely created so I don’t want to save it now, but for test, I’ve tried to save it but the problem still the same. In fact I think my file isn’t considered as a CUploadedFile. In the following code, I have an non-object error on licenceFile.tempName().




$model->licenceFile=CUploadedFile::getInstance($model,'licenceFile');

if($model->save())

{

   echo $model->licenceFile.tempName();

   $model->licenceFile->saveAs($model->licenceFile.tempName());

   die();

}

You need to read that article linked to. :)

CUploadedFile is separate from the model, so it’s not saved if you save the model.

Not sure to understand, little recap just to be sure:

My model t_licence has been created with all my database field (f_licence_id, f_product_id…)

But, to upload I’ve created a variable licenceFile like that:


class t_esi_licence extends CActiveRecord

{

	public $licenceFile;

So, when I save my model, $licenceFile is not saved, that why I have a non-object error. Am I right? :blink:

What am I supposed to do to correct that? How should I save CUploadedFile? Am I obliged to save my model t_licence?

Sorry, I’m a little lost! :)

Okay… Did you [size="7"]READ[/size] the article Kokomo linked you to?

Yes, I follow this article, but apparently, there is something I have misunderstood because I don’t see the difference between my code and the article code.

First declare an attribute to store the file name in the model class => Done.

Also declare a file validation rule for this attribute => Done.

My controller is similar to the on in the article:




...

$model->licenceFile=CUploadedFile::getInstance($model,'licenceFile');

if($model->save())

{

   echo $model->licenceFile.tempName();

   $model->licenceFile->saveAs($model->licenceFile.tempName());

   die();

}

And my view send me the right $_FILES. So I don’t see where is the problem.

Ok, I see, I can’t use this syntax in my view:


<?php echo CHtml::beginForm(); ?>

<?php echo CHtml::label('Insert licence file','file'); ?>

<?php echo CHtml::fileField('file',''); ?>

but this one instead:


echo $form->labelEx($model, 'licenceFile');

echo $form->fileField($model, 'licenceFile');

In the first case getInstance($model,‘licenceFile’) is null, that why I have an error.

Thanks for your help. ;)

Ouch. I didn’t spot that. Yup, you’re right. ;)