简体   繁体   中英

Multiple Image Upload only uploading one image in Symfony 3?

I'm building a function to upload multiple images to our website, and so far I have this as my upload form:

<?php
namespace Tyson\AdminBundle\Form;

use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;

use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;

class IuType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {

        $builder->add('id', IntegerType::class);
        $builder->add('image', FileType::class, array(
            'attr' => array(
                'multiple' => 'true',
                'data_class' => null
            )
        ));
    }

    public function getName()
    {
        return 'iu';
    }

}

This is then displayed on the Twig file like this:

  <div class="tabs-panel" id="panel3v">
      {{ form_start(iu,{action:path('admin_upload-images')}) }}

      <div class="row">
          <div class="small-3 medium-3 large-3 columns field-label">
              <label>Image ID</label>
          </div>
          <div class="small-9 medium-9 large-9 columns">
              {{ form_widget(iu.id) }}
              {{ form_errors(iu.id) }}
          </div>
      </div>

      <div class="row">
          <div class="small-3 medium-3 large-3 columns field-label">
              <label>Image Upload</label>
          </div>
          <div class="small-9 medium-9 large-9 columns">
              {{ form_widget(iu.image) }}
              {{ form_errors(iu.image) }}
          </div>
      </div>

      <div class="row">
          <div class="small-6 medium-6 large-6 columns">
              <button type="submit" class="button" id="saveBtn">Save Changes</button>
          </div>
      </div>

      {{ form_end(iu) }}
  </div>

When this is submitted, during testing, I'm outputting what is handed to the controller by the form. This is the output:

[files] => Symfony\Component\HttpFoundation\FileBag Object
    (
        [parameters:protected] => Array
            (
                [iu] => Array
                    (
                        [image] => Symfony\Component\HttpFoundation\File\UploadedFile Object
                            (
                                [test:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 
                                [originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => CnY8dRqXYAAoaAw.jpg
                                [mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => image/jpeg
                                [size:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 35164
                                [error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 0
                                [pathName:SplFileInfo:private] => /tmp/phpibZuVW
                                [fileName:SplFileInfo:private] => phpibZuVW
                            )

                    )

            )

I could select one image, or 100 images, and still the form is only passing me one image before the controller does anything else.

For reference, this is the code for my controller so far:

<?php

namespace Tyson\AdminBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\Security\Core\User\UserInterface;

use Tyson\CoreBundle\Entity\Images;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;

use Symfony\Component\HttpFoundation\File\UploadedFile;

class UploadController extends Controller
{
    public function uploadimagesAction(Request $request)
    {

        $fs = new Filesystem();

        $dm = $this->getDoctrine()->getManager();

        $dir = '/var/www/html/tyson/web/uploads/images/';

        $request = $this->get('request_stack')->getCurrentRequest();

        $formdet = $request->request->get('iu');
        $imgId = $formdet['id'];

        $folderPath = '/var/www/html/tyson/web/uploads/images/'.$imgId;
        $imgDirectory = '/var/www/html/tyson/web/uploads/images/'.$imgId.'/';

        $folderCheck = $fs->exists($folderPath);

        print_r($request);
        die();

    }

}

I'd imagine that the file output should contain more than one image to the controller (as the form is submitted once, not repeatedly), so what am I doing wrong for this to happen?

我在Symfony 4.2上碰到了这个问题,在fileBag的数组中只获得了1个图像,soltion是运行php bin/console cache:clear在我添加了multiple属性后php bin/console cache:clear

Same problem form me

          $builder->add('files', FileType::class, array('required' => false,
            'mapped' => false,
            'attr' => array(
                'multiple' => true
            ),
        )); 

Only one file is submitted. No solutions for me for now, i will add [] with javascript to the upload name.

Have you tried to use the "multiple" option (not the one in the "attr" option)?

$builder
    ->add('files', FileType::class, [
        'multiple' => true,
        'attr' => [
            'multiple' => 'multiple'
        ]
    ]
;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM