Search code examples
phpsymfonyvichuploaderbundle

Files are not uploaded and temp name is inserted at DB, what I'm doing wrong?


I'm using this bundle for upload files through a form. This is what I have done. In the Twig template where I'm using the bundle

<form action="{{ path('guardar-natural') }}" method="POST" class="form-horizontal" role="form" id="registroNatural" {{ form_enctype(form) }}>
    {{ form_widget(form.documento_rif) }}
    <button type="submit" class="btn btn-primary" id="btnEnviarRegistro">{{ 'registration.submit'|trans }}</button>
</form>

In the form class:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
            ->add('documento_rif', 'vich_file', array(
                'required'      => true,
                'mapping'       => 'recaudos',
                'allow_delete'  => true,
                'download_link' => true,
                'label' => false,
            ))
            ->add('usuario', new RegistrationForm());
}

In the entity where field reside:

use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

/**
 * @Vich\Uploadable
 */
class Natural
{
    /**
     * @Vich\UploadableField(mapping="recaudos", fileNameProperty="documentoRIF")
     * @var File $imageFile
     */
    protected $imageFile;

    /**
     * @ORM\Column(type="string", length=255, name="documento_rif", nullable=true)
     * @var string $documentoRIF
     */
    protected $documentoRIF;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     * @var \DateTime $updatedAt
     */
    protected $updatedAt;

    /**
     * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image
     */
    public function setImageFile(File $image = null)
    {
        $this->imageFile = $image;

        if ($image) {
            $this->updatedAt = new \DateTime('now');
        }
    }

    /**
     * @return File
     */
    public function getImageFile()
    {
        return $this->imageFile;
    }

    /**
     * @param string $documentoRIF
     */
    public function setDocumentoRIF($documentoRIF)
    {
        $this->documentoRIF = $documentoRIF;
    }

    /**
     * @return string
     */
    public function getDocumentoRIF()
    {
        return $this->documentoRIF;
    }
}

At config.yml file:

vich_uploader:
    db_driver: orm # or mongodb or propel or phpcr
    storage:    vich_uploader.storage.file_system
    mappings:
        recaudos:
            uri_prefix:         /recaudos
            upload_destination: %kernel.root_dir%/../web/uploads/recaudos
            namer:              vich_uploader.namer_uniqid
            delete_on_update:   true
            delete_on_remove:   true

The bundle is enabled since I don't get any errors, directory has the right permissions (0755). At DB level when any records is created in documento_rif column I get this string /tmp/php1fbjiZ instead the file name, what is wrong in my code?

Of course I have more code on all this but just write here the relevant part.


Solution

  • In your form type, you should refer to the uploadable field (imageFile) instead of the filename column (documento_rif).

    Not linked to your bug but will cause problems: the uri_prefix defined in your mapping is incorrect, it should be /uploads/recaudos.

    The rest should be okay.