我使用Symfony 3.2并使用文件字段构建我的表单,该字段仅接受jpg,jpeg,gif和png扩展。不幸的是,虽然我在允许的mime-types中提到了image / jpeg mime-type,但我无法上传任何带有.jpg扩展名的图片。显示错误“mimeTypesMessage”。
如果你看一眼,我将不胜感激。感谢
ImageType.php
/**
* Defines the form used to create and manipulate images.
*
* @author XXX
*/
class ImageType extends AbstractType {
/**
* Main function of the class.
*
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('title', TextType::class, array(
'label' => 'Tytuł',
'constraints' => [
new Length([
'min' => 3,
'max' => 128,
'minMessage' => 'Tytuł musi posiadać minimum 3 znaki.',
'maxMessage' => 'Tytuł może zawierać maksymalnie 128 znaków.',
]),
],
))
->add('file', FileType::class, array(
// 'mapped' => false,
'constraints' => [
new Image([
'maxSize' => '2M',
'maxSizeMessage' => 'Maksymalny rozmiar obrazka wynosi 2 MB. Wgraj mniejsze zdjęcie.',
'mimeTypes' => ['image/jpeg,', 'image/pjpeg', 'image/png', 'image/gif'],
'mimeTypesMessage' => 'Nieobsługiwany format pliku. Dozwolone rozszerzenia obrazków to: jpg, gif, png.',
'uploadErrorMessage' => 'Dodawanie obrazka zakończyło się niepowodzeniem.',
'minWidth' => 1000,
'minWidthMessage' => 'Minimalna szerokość obrazka to px',
'minHeight' => 1000,
'minHeightMessage' => 'Minimalna wysokość obrazka to px',
'maxWidth' => 1100,
'maxWidthMessage' => 'Maksymalna szerokość obrazka to px',
'maxHeight' => 1100,
'maxHeightMessage' => 'Maksymalna wysokość obrazka to px',
]),
],
))
->add('source', TextType::class, array(
'label' => 'Źródło',
'required' => false,
))
->add('reset', ResetType::class, array(
'label' => 'Reset'
))
->add('submit', SubmitType::class, array(
'label' => 'Zapisz'
))
;
}
/**
* Configures form options.
*
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Object::class,
]);
}
}
ObjectController.php
public function addImageAction(Request $request) {
$object = new Object();
// create form with multiple buttons
$form = $this->createForm(ImageType::class, $object)
->add('saveAndCreateNew', SubmitType::class);
// fill the object by values from the form (if submitted)
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$file = $object->getFile();
if(!$file->getError()) {
$ext = $file->guessExtension();
if ($ext == 'jpeg') {
$ext = 'jpg';
}
$object->setName(md5(uniqid()) . '.' . $ext );
$object->setFile($file->getClientOriginalName());
$object->setFormat($file->getMimeType());
} else {
$this->addFlash('danger', 'Wystąpił nieznany błąd w przesyłaniu formularza. Spróbuj ponownie.');
return $this->redirectToRoute('app_object_addimage');
}
// rest of form handling...
对我来说很奇怪,IANA页面(http://www.iana.org/assignments/media-types/media-types.xhtml)上未列出image / jpeg mime-type。不幸的是,我还没有在互联网上找到问题解决方案。
答案 0 :(得分:1)
看起来你问题的原因只是一个小错字。插入错误位置的逗号:
'mimeTypes' => ['image/jpeg,', 'image/pjpeg', 'image/png', 'image/gif'],
答案 1 :(得分:0)
尝试使用Image而不是File。它默认接受所有图像类型,并且应该适用于JPG / JPEG:
http://symfony.com/doc/current/reference/constraints/Image.html#mimetypes
答案 2 :(得分:0)
我在$file->guessExtension()
遇到了同样的问题,找不到与您相同的任何解决方案。
直到我找到$file->guessClientExtension()
函数here,并且在与Symfony 3.3.10
交换后它对我有用。
我的代码示例:
$imageForm->add('image', FileType::class, array(
'label' => false,
'attr' => array(
'accept' => "image/*",
'multiple' => "multiple",
),
'required' => true,
'multiple' => true,
));
$imageForm->handleRequest($request);
if ($imageForm->isSubmitted() && $imageForm->isValid()) {
$model = $imageForm->getData();
$files = $model->getImage();
foreach($files as $file) {
$newFileName = $someGeneratedIdentifier.'.'.$file->guessClientExtension();
$file->move(
$this->getParameter('directory').'/'.$someSubdirectory,
$newFileName
);
...