0
return array(
  array('speech', 'required'),
  array('speech,document,speech_on,created_on, updated_on,inactive','safe')
  array('document','file','restrict','types'=>'.php,.aspx,.cs', 'maxSize'=>1024*1024*2,'tooLarge'=>'Size should be less then 2MB','on'=>'upload'),      
);

How can restrict these file to upload,

Justinas
  • 41,402
  • 5
  • 66
  • 96
Umashankar Saw
  • 1,131
  • 1
  • 9
  • 25

2 Answers2

0

add a custom rule like this:

array('document', 'docTypeRuleValidator'),

then create that custom validation rule inside components/validators with the name docTypeRuleValidator as:

class DocTypeRuleValidator extends CValidator
{
    public function validateAttribute($model, $attribute)
    {
        yii::trace($attribute, __METHOD__);
        yii::trace(CVarDumper::dumpAsString($_FILES), __METHOD__);

        if(in_array($_FILES['ModHwTM']['type']['imageUploaded'], array('image/jpg')))
        {
            $this->addError($model, $attribute, 'Not appropiate type!');
        }
    }
}

the trick here is to find the exact parameter of the Files array you want to use to validate type, following php zend certification specs is not enough to use Files, cause it could be manipulated by the user so this is a first validation ... if it pass then you should to check the temp file you have uploaded to make sure it is the type you support, but it is a great start i think.

For example i have made a test an the $_files array returns:

array
(
        'ModHwTM' => array
        (
                'name' => array
                (
                        'imageUploaded' => 'crude_widnows.jpg'
                )
                'type' => array
                (
                        'imageUploaded' => 'image/jpeg'
                )
                'tmp_name' => array
                (
                        'imageUploaded' => '/tmp/phpcYm2Vo'
                )
                'error' => array
                (
                        'imageUploaded' => 0
                )
                'size' => array
                (
                        'imageUploaded' => 6527
                )
        )
)

please read: how $files works

you could use this example to create your own custom validator to check the temp file Please add your comments...

Community
  • 1
  • 1
DiegoCoderPlus
  • 760
  • 6
  • 14
0

I did the coding, what I want:

Added custom rule like this

array('document_path', 'docTypeRule','param'=>array('file'=>$documentName,'restrict'=>array('cs','php','aspx'))),


public function docTypeRule($attribute,$params)
{
    $extn = strtolower(pathinfo($params['param']['file'], PATHINFO_EXTENSION));
    //Put
    if(in_array($extn, $params['param']['restrict'])){
      $this->addError($attribute, 'Invalid file type, .'.$extn.' file not allowed!');
    }
}

Please comment if anything wrong, but this is working fine.

Umashankar Saw
  • 1,131
  • 1
  • 9
  • 25