[right]
سلام
یه فرم اپلود درست کردم
فرم رو اجرا میکنم
وقتی به صفحه که برمیگردد
همون صفحه رو دوباره تکرار میشه
برای حل این کار چیکار باشد کرد.
[/right]
<?php
$this->pageTitle=Yii::app()->name . ' اپلود';
$this->breadcrumbs=array(
"اپلود"
);
$this->menu=array(
);
?>
<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">موارد <span class="required">*</span> الزامی می باشد</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' ); ?>
</div>
<?php $this->endWidget(); ?>
</div>
<?php
class Image extends CActiveRecord
{
public $uploaded_owner_img;
public static function model($className=__CLASS__)
{
return parent::model($className);
}
public function tableName()
{
return 'image';
}
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' => 'نام فایل',
'owner_name' => 'نام'
);
}
protected function beforeSave() {
if (parent::beforeSave ()) {
if ($this->isNewRecord) {
//$this->created = date('Ymd');
}
return true;
}else {
return false;
}
}
}
<?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'=>array(
'class'=>'CViewAction',
)
);
}
public function actionIndex(){
$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 .'/../images/'.$model->owner_img);
$model->data= date('M:y <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/biggrin.gif' class='bbc_emoticon' alt=':D' />');
if($model->save()){
// echo "با موفقیت انجام شد"; die(0);
$this->render('index',array('model'=>$model));
}
}
}
}
$this->render('index',array( 'model'=>$model ));
}
protected function performAjaxValidation($model)
{
if(isset($_POST['ajax']) && $_POST['ajax']==='map-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
}
}