2

I am currently uploading single image file at a time to mysql database, However I need to upload 800 images, so I want to select all images and upload them by one click. This is my PHP controller

/**
     * @Route("/insert", name="satellite_images_create")
     */
    public function insertAction(Request $request)
    {
        $satelliteImage=new satelliteImage;

        $form=$this->createFormBuilder($satelliteImage)
            ->add('file')

            ->add('save',SubmitType::class,array('label'=>'Insert Image','attr'=>array('class'=>'btn btn-primary','style'=>'margin-bottom:15px')))
            ->getForm();

        $form->handleRequest($request);

        if ($form->isSubmitted()  && $form->isValid()) {
            $em=$this->getDoctrine()->getManager();

            $satelliteImage->upload();

            $em->persist($satelliteImage);
            $em->flush();

            $this->addFlash(
                'notice',
                'Image inserted successfully'
                );

            return $this->redirectToRoute('satellite_images');
        }

        return $this->render('satelliteImages/insert.html.twig',array(
            'form'=>$form->createView()));
    }

SatelliteImage ENTITY has a function to handle the upload

public function upload()
    {
    // the file property can be empty if the field is not required
    if (null === $this->getFile()) {
        return;
    }

    $imgFile=$this->getFile();
    $this->setImage(file_get_contents($imgFile));

    $this->getFile()->move(
        $this->getUploadRootDir(),
        $this->getFile()->getClientOriginalName()
    );

    // set the path property to the filename where you've saved the file
    $this->path = $this->getFile()->getClientOriginalName();

    // clean up the file property as you won't need it anymore
    $this->file = null;
    }

And here is my twig file

{% extends 'base.html.twig' %}

{% block body %}
    <h2 class="page-header">Insert Image</h2>
    {{ form_start(form) }}
    {{ form_widget(form) }}
    {{ form_end(form) }}

{% endblock %}

How I modify the form and the upload function so that I can select all the image files to be uploaded?Thanks.

DarkBee
  • 16,592
  • 6
  • 46
  • 58
Madhuka Harith
  • 175
  • 1
  • 2
  • 13

1 Answers1

1

I would recommend checking out DropzoneJS http://www.dropzonejs.com/

It is a javascript front end for handling multiple file uploads. It works by passing the files to a PHP backend for storage / processing.

EDIT - ADDITION

If you need info on how to use DropzoneJS with symfony2 then check out Multi upload Symfony 2.3 with Dropzone

Also, this question is basically a duplicate of Problems With Multiple File Upload In Symfony2

Community
  • 1
  • 1
infinigrove
  • 489
  • 1
  • 7
  • 16
  • Front end is fine, My problem is how I am going to handle them in PHP backend – Madhuka Harith Jan 08 '17 at 14:24
  • Basically you need to call your `upload()` function for each file. Your front end form should send each file individually. That is how DropzoneJS works. If you try to send all 800 images in one big POST you are likely to have issues with `upload_max_filesize` and `post_max_size`. – infinigrove Jan 08 '17 at 15:06
  • Also I would say that if your PHP backend currently handles a single file then the backend is fine and the problem is with your front end. You just need to make the front end send each file to the PHP backend individually. – infinigrove Jan 08 '17 at 16:44
  • Yeah I am able to upload single image. But the question is, how can I send multiple files to the back end.If that is possible I can simply call upload() function for each file.From what I understand, Form is created for one entity.Is that correct? – Madhuka Harith Jan 08 '17 at 17:39
  • In your form you need a field for each file that you want to upload. The way DropzoneJS works is that it dynamically creates a field for each file that you add and processes(submits) the form for that dynamically field therefore calling your upload() function for each file. – infinigrove Jan 08 '17 at 18:47
  • 1
    There may possibly be a way that you can use as jeff suggested. However, you will have process the images as an array in your controller and I'm not sure how you would do that. You could check out the lookphp at gmail dot com example in the PHP documentation @ http://php.net/manual/en/features.file-upload.multiple.php#119143 for more info. – infinigrove Jan 08 '17 at 20:03