Showing posts with label upload image. Show all posts
Showing posts with label upload image. Show all posts

Sunday, April 21, 2013

Yii upload image or file to server

Model Image:
<?php
class Image extends CActiveRecord
{
    public $uploaded_owner_img;
  
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }
    
    public function tableName()
    {
        return 'table_name';
    }
    
    public function rules()
    {
        return array(
            array('owner_img', 'file', 'allowEmpty'=>true,'types'=>'jpg','maxSize'=>1024*1024*1, 'tooLarge'=>'Image must be less than�1MB'),
            array('owner_name, owner_img', 'required'),
            array('owner_name', 'unique'),
            array('owner_name', 'length', 'max'=>20, 'encoding'=>'utf-8'),
            array('owner_name', 'safe', 'on'=>'search'),
        );
    }
    
    public function attributeLabels()
    {
        return array(
            'owner_img' => 'Select image.',
            'owner_name' => 'Name'
        );
    }
    
    protected function beforeSave() {
        if (parent::beforeSave ()) {
            if ($this->isNewRecord) {
                $this->created = date('Ymd');
            }
            return true;
        }else {
            return false;
        }
    }
}
Controller ImageController.php
<?php
class ImageController extends Controller
{
    public function actions()
    {
        return array(
            // captcha action renders the CAPTCHA image displayed on the contact page
            'captcha'=>array(
                'class'=>'CCaptchaAction',
                'backColor'=>0xFFFFFF,
            ),
            // page action renders "static" pages stored under 'protected/views/site/pages'
            // They can be accessed via: index.php?r=site/page&view=FileName
            'page'=>array(
                'class'=>'CViewAction',
            )
        );
    }
        
    public function actionCreate()
    {
        $model=new Image;
        //AJAX validation is needed
        $this->performAjaxValidation($model);
        if(isset($_POST['Image'])) {
            $model->attributes=$_POST['Image'];
            if (@!empty($_FILES['Image']['name']['owner_img'])){
                $model->owner_img = time().".jpg";
                if ($model->validate(array('owner_img'))){
                    $model->uploaded_owner_img = CUploadedFile::getInstance($model, 'owner_img');
                    $model->uploaded_owner_img->saveAs(Yii::app()->basePath.'/'.$model->owner_img);
                    
                    if($model->save()){
                        echo "Model saved"; die(0);
                    }
                }
            }
        }

        $this->render('create',array(
            'model'=>$model,
        ));
    }
    
    protected function performAjaxValidation($model)
    {
        if(isset($_POST['ajax']) && $_POST['ajax']==='map-form')
        {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }
}
View file
<?php
$this->breadcrumbs=array(
    "image/create"
);

$this->menu=array(
);
?>

<h1>Image Upload</h1>

<div class="form">

<?php 
$form=$this->beginWidget('CActiveForm', array(
    'id'=>'map-form',
    'enableAjaxValidation'=>false,
    'htmlOptions' =>array('enctype'=>"multipart/form-data" ),
    'enableAjaxValidation' => false,
    'enableClientValidation'=>true,
    'clientOptions'=>array('validateOnSubmit'=>true)
)); 
?>

<p class="note">Mark <span class="required">*</span> are required.</p>

<?php echo $form->errorSummary($model); ?>

<div class="row">
    <?php echo $form->labelEx($model,'owner_name'); ?>
    <?php echo $form->textField($model,'owner_name',array('size'=>32,'maxlength'=>20)); ?>
    <?php echo $form->error($model,'owner_name'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model,'owner_img'); ?>
    <?php echo $form->fileField($model,'owner_img'); ?>
    <?php echo $form->error($model,'owner_img'); ?>
</div>

<div class="row buttons">
    <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>

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