0

Hi please check my following code:

Model:

 public $image;
/**
 * @return string the associated database table name
 */
public function tableName()
{
    return 'file';
}

/**
 * @return array validation rules for model attributes.
 */
public function rules()
{
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
        array('image_url', 'required'),
        array('image_url', 'length', 'max'=>256),
        // The following rule is used by search().
        // @todo Please remove those attributes that should not be searched.
        array('image_url', 'safe', 'on'=>'search'),
         array('image', 'file', 'types'=>'jpg, gif, png'),
    );
}

Controler:

public function actionCreate()
{
    $model=new File;
            print_r($_POST['File']);
             $this->render('create',array(
        'model'=>$model,
    ));
}

View:

<?php
 $form = $this->beginWidget(
'CActiveForm',
array(
    'id' => 'upload-form',
    'enableAjaxValidation' => false,
    'htmlOptions' => array('enctype' => 'multipart/form-data'),
)
);

   echo $form->labelEx($model, 'image');
   echo $form->fileField($model, 'image');
   echo $form->error($model, 'image');
   echo CHtml::submitButton('Submit'); 
   $this->endWidget();?>

you can see print_r($_POST['File']); in my controller. It gives me empty array with no url or anything regarding the uploaded image. Please help

Thanks

Solution

following changes were made in the controller function which worked for me.

public function actionCreate()
{
    $model=new File;
            $model->image_url = CUploadedFile::getInstance($model,'image');


            if($model->save())
            $model->image_url->saveAs('D:/xampp/htdocs/project/images/upload/logo.jpg');


    $this->render('create',array(
        'model'=>$model,
    ));
}

1 Answer 1

1
$model = new Photo;
$model->attributes = $_POST['Photo'];
$model->image = CUploadedFile::getInstance($model,'image');
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.