Yii Framework Forum: Uploading Multiple Image In Yii - Yii Framework Forum

Jump to content

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

Uploading Multiple Image In Yii Rate Topic: -----

#1 User is offline   Messi 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 26-March 14

  Posted 28 March 2014 - 07:42 AM

I have been trying to upload multiple file i.e. image.
my model Student.php looks like

class Student extends CActiveRecord {

//    public $photo;

    function tableName(){
        return 'student';
    }

    function rules(){
        return array(
            array('name,address,roll,photo,image ','required'),
            array('photo', 'file'),
            array('image'),
        );
    }
}


my Controller looks like

public function actionSaveStudent(){
       // $model=new Student;

        $model=new Student();  // this is my model related to table
//   
        $this->render('newstudent',array(
            'model'=>$model,
        ));

        if(isset($_POST['submit']))
        {
            $model->name=$_POST['name'];
            $model->address=$_POST['address'];
            $model->roll=$_POST['roll'];
            $rnd = rand(0,9999);  // generate random number between 0-9999
            //$model->attributes=$_POST['Student'];


            $uploadedFile=CUploadedFile::getInstance($model,'photo');
            $uploadedFile=CUploadedFile::getInstance($model,'image');
            $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
            $model->photo = Yii::app()->basePath.'/student/'.$fileName;
            $model->image = Yii::app()->basePath.'/created_htdocs/'.$fileName;
            $uploadedFile->saveAs(Yii::app()->basePath.'/../student/'.$fileName);



            $uploadedFile->saveAs(Yii::app()->basePath.'/../created_htdocs/'.$fileName);


            // $model->photo = $fileName;




                if($model->validate())
            {
                if($model->save())
                {
                    echo "saved";
                }
                else
                    echo 'failure';
            }
            else
                print_r($model->getErrors());

        }
       // $this->render("newstudent");
    }
}
?>


and my view looks like

<form action="savestudent"  enctype="multipart/form-data" method="post">

   <label> Name: <input name="name" type="text" ></label> <br><br>
   <label> Roll: <input name="roll" type="text" ></label><br><br>
   <label>Address: <input name="address" type="text" ></label><br><br>
    <label>Upload_photo:</label>
        <?php echo CHtml::activeFileField($model,'photo'); ?><br><br>[attachment=5384:newstudent.php]
    <?php echo CHtml::activeFileField($model,'image'); ?><br><br>
    <input type="submit" name="submit" id="submit" value="Save">

</form>


and i got error Property "Student.image" is not defined.

This post has been edited by Keith: 28 March 2014 - 07:46 AM
Reason for edit: Added code tags.

0

#2 User is online   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,595
  • Joined: 04-March 10
  • Location:UK

Posted 28 March 2014 - 07:48 AM

This line is causing that error:

<?php echo CHtml::activeFileField($model,'image'); ?>


This still won't work while the $photo attribute of Student is commented out though.
0

#3 User is online   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,595
  • Joined: 04-March 10
  • Location:UK

Posted 28 March 2014 - 07:49 AM

I've deleted your duplicate thread. If you wish to include the files you attached again, you should update your first post here to add them.
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