CFileValidator validate a file

Hi,

I used the following code to unzip some files:




    private function _processZip($file) {

        $zip=new ZipArchive;

        if ($zip->open($file)===true) {

            $zip->extractTo($this->unzippedDir);

            $zip->close();

            return true;

        }

        return false;

    }



The goal is to scan the destination directory and validate each file.

Then I’ll store these validated files and auto-delete the non-validated ones.

Of course, i would like to reuse my file validation rule i setted up in the model:




public function rules() {

        return array(array('title','file','on'=>'add','allowEmpty'=>false,'types'=>Yii::app()->params['allowedTypes'],'wrongType'=>'Wrong file type !','minSize'=>Yii::app()->params['minUploadSize'],'tooSmall'=>'File is too small !','maxSize'=>Yii::app()->params['maxUploadSize']),

...



is Yii able to do that ?

How i can access this rule ?

You could create your own validation rule for this:




public function rules() {

   return array(

      array('file', 'zipnvalidate')

   );

}


public function zipnvalidate() {

   //do unzip and get files in an array

   foreach($files as $file) {

      $validator = new CFileValidator;

      $validator->attributes = array($file);

      $validator->types = Yii::app()->params['allowedTypes'];

      //and so on

      if($validator->validate()) {

         //yeah, everything is ok with this one

      }

      else {

         //validation error, better delete this file

         unlink($file);

      }

   }

   return true;

}



thx