文件类型验证不起作用

时间:2013-01-19 08:42:52

标签: php yii

我创建了表单来上传文件,还建模以验证类型。 如果我上传php文件或任何文件将成功上传, 但在模型中我添加了文件类型 只是“jpg,gif,png,pdf,rar,zip,doc,docx”

如何解决?

观点:

.......
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'Cfiles-form',
   /*
        'enableAjaxValidation'=>true,
        'enableClientValidation'=>true,
        'clientOptions'=>array('validateOnSubmit'=>true), //This is very important
       */
       'clientOptions'=>array('validateOnSubmit'=>true), //This is very important
       'htmlOptions' => array('enctype' => 'multipart/form-data'),
)); ?>

<?php
/// difine Section model 
$models=new Courses;
?>
<?php echo $form->errorSummary($model,'يرجى تعديل الأخطاء التالية'); ?>
<table>
<h3> إضافة ملفات الدورات</h3>
<tr>
    <td>
        <div class="row">
        <?php echo $form->labelEx($model,'title'); ?>
        <?php echo $form->textField($model,'title',array('size'=>60,'maxlength'=>255)); ?>
        <?php echo $form->error($model,'title'); ?>
    </div>

    </td>
</tr>
<tr>
    <td>
            <div class="row">
        <?php echo $form->labelEx($model,'desc'); ?>
        <?php echo $form->textField($model,'desc',array('size'=>60,'maxlength'=>600)); ?>
        <?php echo $form->error($model,'desc'); ?>
    </div>
    </td>
</tr>
<tr>
    <td>
    <div class="row">
        <?php echo $form->labelEx($model,'file'); ?>
        <?php echo $form->fileField($model,'file'); ?>
        الملفات المسموحة : jpg,gif, png,pdf,rar,zip,doc,docx
        <?php echo $form->error($model,'file'); ?>
    </div>
    </td>
</tr>
.......

型号:

....
public function rules(){
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('title, desc,  privacy', 'required','message'=>'يرجى ملئ حقل {attribute}  '),
            array('privacy', 'numerical', 'integerOnly'=>true),
            array('title', 'length', 'max'=>255),
            array('desc', 'length', 'max'=>600),

             array('file', 'file', 'types'=>'jpg,gif, png,pdf,rar,zip,doc,docx','allowEmpty'=>true, 'on'=>'update'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('f_id, title, desc, file, privacy', 'safe', 'on'=>'search'),
        );
    }
.....

控制器:

public function actionCreate()
    {
        $model=new Cfiles;

        $models= new Courses;

        // Uncomment the following line if AJAX validation is needed
    $this->performAjaxValidation($model);

        if(isset($_POST['Cfiles'])){
            $model->attributes=$_POST['Cfiles'];
            $valdiate=$model->validate();

         /////// upload image functions 
         $rnd = rand(0,999984375);  // generate random number between 0-9999
         $model->attributes=$_POST['Cfiles']['file'];
         $uploadedFile=CUploadedFile::getInstance($model,'file');
        if(!empty($uploadedFile)){
            $ext=$uploadedFile->getExtensionName();
            $fileName = "samilox$rnd.{$ext}";  // random number + file name
            }

             ////////// end 

            if($model->save()){
                    if(!empty($uploadedFile))  // check if uploaded file is set or not
                {
                 $uploadedFile->saveAs(Yii::app()->basePath.'/../cfillaf/'.$fileName);  // upload image to server 
                  $model->file = $fileName;

                  $model->save(false);
               }   

              echo " Work ";
              $this->redirect(array('cfiles/admin'));
       }
                else{

          echo " error";

                $this->redirect(array('cfiles/admin'));
                }
        }

         $this->layout='adminsidebar';
         if(Yii::app()->request->getIsAjaxRequest())
          echo $this->renderPartial('_form',array('model'=>$model),true,true);//This will bring out the view along with its script.

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

        }

提前致谢

2 个答案:

答案 0 :(得分:1)

删除模型文件规则函数中的'on'=&gt;'update'以解决您的问题。

array('file','file','types'=&gt;'jpg,gif,png,pdf,rar,zip,doc,docx','allowEmpty'=&gt; true,'on '=&GT;' 更新'),

答案 1 :(得分:0)

感谢Bool.dev

是的,我忘记在插入方案中添加验证

代码:

array('file', 'file', 'types'=>'jpg,gif, png,pdf,rar,zip,doc,docx','allowEmpty'=>false, 'on'=>'insert'),