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.

2
  • 1
    Your template should produce the necessary HTML to allow multiple file uploads i.e. <input type='file' name='myFiles' multiple> Commented Jan 8, 2017 at 10:09
  • That can be done.But how do I handle it in the controller? Commented Jan 8, 2017 at 10:26

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

6 Comments

Front end is fine, My problem is how I am going to handle them in PHP backend
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.
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.
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?
There may possibly be a way that you can use <input type='file' name='myFiles[]' multiple> 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 @ php.net/manual/en/features.file-upload.multiple.php#119143 for more info.
|

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.