Yii Framework Forum: File Upload - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

File Upload Rate Topic: -----

#1 User is offline   cyanatide 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 10-August 11

Posted 01 March 2013 - 09:39 AM

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.
0

#2 User is offline   kokomo 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 289
  • Joined: 23-July 10

Posted 01 March 2013 - 09:51 AM

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.
0

#3 User is offline   cyanatide 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 10-August 11

Posted 01 March 2013 - 10:09 AM

View Postkokomo, on 01 March 2013 - 09:51 AM, said:

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

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();
}

0

#4 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 01 March 2013 - 10:24 AM

You need to read that article linked to. :)

CUploadedFile is separate from the model, so it's not saved if you save the model.
"Less noise - more signal"
0

#5 User is offline   cyanatide 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 10-August 11

Posted 01 March 2013 - 11:26 AM

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! :)
0

#6 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 01 March 2013 - 11:30 AM

Okay.. Did you READ the article Kokomo linked you to? Posted Image
"Less noise - more signal"
0

#7 User is offline   cyanatide 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 10-August 11

Posted 03 March 2013 - 10:52 AM

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.
0

#8 User is offline   cyanatide 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 10-August 11

Posted 04 March 2013 - 04:44 AM

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. ;)
0

#9 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 04 March 2013 - 06:41 AM

Ouch. I didn't spot that. Yup, you're right. ;)
"Less noise - more signal"
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users