1

I've been trying to upload multiple files in a form but I just cannot find a solution that would work for me. Whenever I add multiple files to my input, only one of those files gets passed to the controller and uploaded.

Controller.php

        // File uploading
        $files = $auto->getImages();
        $images = array();

        if($files != null) {
            $key = 0;

            foreach ($files as $file)
            {
                $fileName = md5(uniqid()) . '.' . $file->guessExtension();
                $file->move(
                    $this->getParameter('car_adds'),
                    $fileName
                );
                $images[$key++] = $fileName;
            }
            $auto->setImages($images);
        }

$auto->getImages() gives me a string value instead of an array value, therefor, whenever I try to foreach loop it I get an empty result and no file upload.

I tried renaming my image form widget to "form[images][]" instead of "form[images]" which is generated by the formbuilder however when I do that I get the following error: enter image description here

FormType.php

          ->add('images', FileType::class, array(
            'label' => false,
            'data_class' => null,
            'label_attr' => array('class' => $label_offset),
            'required' => false,
            'attr' => array(
                'class' => $styles,
                'multiple' => true)))

Entity.php

    /**
* @var array
*
* @Assert\Image(
*     mimeTypes = {
*   "image/png",
*   "image/pjpeg",
*   "image/jpeg",
*   "image/gif"
* },
*     mimeTypesMessage="Failas yra netinkamo formato",
*     maxSize = "10M",
*     maxSizeMessage="Failas yra per didelis"
* )
* @ORM\Column(name="images", type="array", nullable=true)
*/
private $images;

 /**
 * Set images
 *
 * @param array $images
 *
 * @return Auto
 */
public function setImages($images)
{
    $this->images = $images;

    return $this;
}

/**
 * Get images
 *
 * @return array
 */
public function getImages()
{
    return $this->images;
}

This is my first project that I'm trying to build with Symfony 3 so I'm assuming there must be something that I may have overlooked.

1 Answer 1

3

You should add multiple to your FileType field options:

 ->add('images', FileType::class, array(
        'multiple' => true, // here it is
        'label' => false,
        ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.