Search code examples
phpsymfony4sonata-adminvichuploaderbundle

The file "" does not exist (VichUploaderBundle)


My problem is that when i try to upload an mp3 file using VichUploadBundler in combination with sonata-admin, i get the following error when i upload the file.

"The file "" was not found"

I aleady have another form inside my project where i upload some images and i have no problem with them, so im quite shock, because i thought that audio files would be easier

Here is my Entity file

/**
* 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;
}

My vich_uploader.yaml file

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

And finally my admin class

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

I just want to be able to see my audio files on my folder, just like the images, im pretty sure is something silly, if you guys need to see something else in my code, just ask so i can update the question


Solution

  • I had to change some things in order to fix my problem

    First of all, i added a new property in the entity called "$audioSize", like this:

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

    I had to change my audioFile property as well, like so...

    /**
    * 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;
    

    and finally in the project php.ini file i had to change the upload max size of the files

    post_max_size = 1024M
    upload_max_filesize = 1024M
    

    I will leave it here for future reference in case someone else encounters the same issue.

    p.d Special thanks to @Iaviku who gave me the idea of adding those fields that at the end solved my problem.