如何使用Symfony 4上传音乐

问题描述

首先,我为我的英语道歉...

我的音乐上传出现问题。我感谢Symfony Doc,如何上传文件,并成功设置图片上传。但是我发现音乐上传my dump in my controller indicates to me an errorand tells me that my file exceeds 2M出现问题,但是在我的文件php.ini中,upload_max_filesize的定义为256M。我上传音乐的代码与图片相同。

我的代码:

namespace App\Form;

use App\Entity\Artist;
use App\Entity\Category;
use App\Entity\Music;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints\File;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\FileType;

class MusicType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder,array $options)
    {
        $builder
            ->add('title')
            ->add('description')
            ->add('picture',FileType::class,[
                'mapped' => false,'required' => false,'constraints' => [
                    new File([
                        'maxSize' => '8M','mimeTypes' => [
                            'image/jpeg','image/png','image/webp'
                        ]
                    ])
                ],'attr' => [
                    'accept'=>'.jpg,.jpeg,.png,.gif'
                ]
            ])
            ->add('music','constraints' => [
                    new File([
                        'maxSize' => '256M','mimeTypes' => [
                            'application/octet-stream','audio/mpeg','audio/mp3'
                        ]
                    ])
                ]
            ])

我一直在寻找一种解决方法,但是所有结果都使我回到了https://symfony.com/doc/current/reference/constraints/File.html#maxsize

如果您有想法,我很感兴趣。

解决方法

首先,您需要在php.ini中检查post_max_size。

upload_max_filesize是任何单个文件的限制。 post_max_size是整个请求的限制,其中可能包含多个文件。

确保post_max_size> = upload_max_size

请参阅以前的答案。

PHP post_max_size overrides upload_max_filesize

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...