Yii 验证中的文件类型 strtolower


file type strtolower in yii validation

有没有办法在 yii 表单验证中将文件类型格式转换为小写。例如,JPG => jpg,PNG => png。甚至,我在验证规则中以大写字母添加了图像文件格式,但它仍然说无效文件格式。

这是我的验证规则:

array('photo','filter','filter'=>'strtolower'),
array('photo', 'file', 'types' => 'JPG,jpg,jpeg,JPEG,gif,GIF,png,PNG', 'allowEmpty' => true, 'maxSize' => 1024 * 1024 * 2,
                'tooLarge' => 'Size should be less then 2MB !!!'),

扩展名称在 CFileValidator 中不区分大小写,因此您可以只添加小写扩展名称,也可以添加 mimeType 以进行正确的验证,如,

array('photo', 'file', 
    'types' => 'jpg, jpeg, gif, png', 
    'allowEmpty' => true, 
    'mimeTypes' => 'image/gif, image/jpeg, image/png'
    'wrongMimeType' => 'Invalid mime type',
    'maxSize' => 1024 * 1024 * 2,
    'tooLarge' => 'Size should be less then 2MB !!!'
)