文件“”不存在(VichUploaderBundle)

时间:2019-04-16 20:33:12

标签: php symfony4 sonata-admin vichuploaderbundle

我的问题是,当我尝试使用VichUploadBundler结合Sonata-admin上载mp3文件时,上传文件时出现以下错误。

"The file "" was not found"

我在项目中还有一个表格,我上载了一些图像,但是我对它们没有问题,所以非常震惊,因为我认为音频文件会更简单

这是我的实体文件

/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
*
* @Vich\UploadableField(mapping="episode_audio", fileNameProperty="audioName")
*
* @var File
*/
private $audioFile;

/**
* @ORM\Column(type="string", length=255)
*
* @var string
*/
private $audioName;

/**
* @ORM\Column(type="datetime")
*
* @var \DateTime
*/
private $updatedAt;

/**
* If manually uploading a file (i.e. not using Symfony Form) ensure an instance
* of 'UploadedFile' is injected into this setter to trigger the update. If this
* bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
* must be able to accept an instance of 'File' as the bundle will inject one here
* during Doctrine hydration.
*
* @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $audioFile
*/
public function setAudioFile(?File $audioFile = null): void
{
   $this->audioFile = $audioFile;
   if (null !== $audioFile) {
       // It is required that at least one field changes if you are using doctrine
       // otherwise the event listeners won't be called and the file is lost
       $this->updatedAt = new \DateTimeImmutable();
   }
 }

public function getAudioFile(): ?File
{
   return $this->audioFile;
}

public function setAudioName(?string $audioName): void
{
    $this->audioName = $audioName;
}

public function getAudioName(): ?string
{
    return $this->audioName;
}

我的vich_uploader.yaml文件

vich_uploader:
    db_driver: orm

    mappings:
        ..//

        episode_audio:
            uri_prefix: /audio
            upload_destination: '%kernel.project_dir%/public/audio/'
            delete_on_update: true
            delete_on_remove: true
            inject_on_load: true

最后是我的管理员班

    protected function configureFormFields(FormMapper $formMapper) {
    $formMapper
      ->add('audioName')
      ->add('audioFile', VichFileType::class, [
        'label' => 'Audio'
      ]);
    }

我只希望能够看到我的音频文件在文件夹中,就像图像一样,我很确定这是很愚蠢的,如果你们需要在我的代码中看到其他内容,请问一下,以便我可以更新问题

1 个答案:

答案 0 :(得分:0)

为了解决我的问题,我不得不进行一些更改

首先,我在名为“ $ audioSize”的实体中添加了一个新属性,如下所示:

 /**
 * @ORM\Column(type="integer")
 *
 * @var integer
 */
private $audioSize;

//getters and setters ...

我也必须更改我的audioFile属性,就像这样...

/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
*
* @Assert\File(
*     maxSize="1024M",
*     mimeTypes={"audio/mpeg", "audio/mp3"}
* )
* @Vich\UploadableField(mapping="episode_audio", fileNameProperty="audioName", size="audioSize")
*
* @var File $audioFile
*/
private $audioFile;

最后在项目php.ini文件中,我不得不更改文件的最大上传大小

post_max_size = 1024M
upload_max_filesize = 1024M

如果其他人遇到相同的问题,我将其留在此处以供将来参考。

p.d特别感谢@Iaviku,他给了我添加最后解决了我问题的字段的想法。