Set Mime_type validation in Symfony

放肆的年华 提交于 2019-12-03 21:56:19

You seem to be using the API incorrectly. The signature of sfForm::setValidator() is here.

However, here's a simple way to set a file validator that allows only web images to be uploaded:

$this->validatorSchema['my_file'] = new sfValidatorFile(array(
    'mime_types' => 'web_images'
), array(
    'invalid'    => 'Invalid file.',
    'required'   => 'Select a file to upload.',
    'mime_types' => 'The file must be of JPEG, PNG or GIF format.'
));

The 'web_images' category includes the following MIME types:

image/jpeg
image/pjpeg
image/png
image/x-png
image/gif

If you want to explicitly define the MIME types to accept, replace 'web_images' with an array of types like this:

'mime_types' => array(
    'image/jpeg',
    'image/pjpeg',
    'image/png',
    'image/x-png',
    'image/gif'
)

There is a way to constraint file selection from upload in a browser.
The drawback is it uses flash to select file, and limit support to users having flash player installed. The advantage is flash give you much more control over upload (parrallel upload, progression, errors, controle files extension allowed to select, etc.).

The component I use for this is swfupload

EDIT : this does not exempt from doing server-side control on uploaded files, but used in combination it offers a similar security leel and a much better user experience (errors can happens before a long-running upload start)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!