Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mughalles's avatar

Form with multiple files

Hello,

I used L5.4 with this Form to upload data to two tables 'posts'->title and 'images'->name for Files,

<form action="{{route('dropzone.store')}}" enctype="multipart/form-data" class="dropzone" id="imageupload">
                {{csrf_field()}}

                    <div class="form-group">
                        <label> Title: </label>
                        <input type="text" name="title" class="form-control">
                    </div>
                
                    <div class="dz-message">

                    </div>

                    <div class="dropzone-previews form-group" id="dropzonepreview" style="padding: 10px; border:solid 1px blue; cursor: pointer;">
                        Click here to upload images
                    </div>

                <input type="submit" name="" id="submitfiles" value="Submit" class="btn btn-success ">
            
            </form>

setttings of Dropzone:

var photo_counter = 0;
Dropzone.options.imageupload = {

    clickable:'#dropzonepreview',
    uploadMultiple: false,
    parallelUploads: 100,
    maxFilesize: 8,
    maxFiles: 3,
    autoProcessQueue: false,
    previewsContainer: '#dropzonepreview',
    previewTemplate: document.querySelector('#preview-template').innerHTML,
    addRemoveLinks: true,
    dictRemoveFile: 'Remove',
    dictFileTooBig: 'Image is bigger than 8MB',

    // The setting up of the dropzone
    init:function() {

      var myDropzone = this;

      $('#submitfiles').on("click", function (e) {

        e.preventDefault();
        e.stopPropagation();

        if(myDropzone.getQueuedFiles().length > 0){
          myDropzone.processQueue();
        }else{
          alert('No Files to upload!');
        }

      });

    },

     success:function(file, response) {
        window.location.href = "../home";

    }
}

HomeController,

<?php

namespace App\Http\Controllers;
use App\Http\Requests;
use Illuminate\Http\Request;
use App\Image;
use App\Post;

class HomeController extends Controller
{

    /**
     * Generate Image upload View
     *
     * @return void
     */

    public function index()
    {
        return view('home');
    }

    public function dropzone()
    {
        return view('dropzone-view');
    }

    /**
     * Image Upload Code
     *
     * @return void
    */ 
    public function dropzoneStore(Request $request)
    {
        $file = $request->file('file');
        $imageName = time().$file->getClientOriginalName();
        $file->move(public_path('images'),$imageName);

        $dbimg = new Image;
        $dbimg->name = $imageName;
        $dbimg->save();
        

        $post = new Post;
        $post->title= $request->title;
        $post->save();

    }
}

everything works fine with the upload the files and insert the name into database. My Problem is that I get the 'title' into the database like the count of the uploaded files.

any help please?

0 likes
9 replies
shakti's avatar

Sorry but your question is not clear

Snapey's avatar

use three backticks ``` before and after blocks of code so that we can see what you are on about

I guess you never hit dropzoneStore since console.log($file = $request->file('file')); is not valid.

mughalles's avatar

Sorry guys,

I must say, I'm newbie with php and Laravel. My problem is, in the form when I select 3 images to upload, I get the value of input title three times saved in the table posts. I tried with foreach through the images, but it didn't work.

Snapey's avatar

You only have one input field? How could you have three different titles?

mughalles's avatar

yes, only one. Why? something wrong with my code. I get title-entities equal the count of the uploaded images.

mughalles's avatar

you make me crazy :-)

I need only one title-entity

Snapey's avatar

ha.. good luck with that one..

(I too can change the response afterwards)

Lazy questions get lazy answers

1 like
jlrdw's avatar
<form action='' method='post' enctype="multipart/form-data">
        <?php
        $num = 0;
        $num_uploads = 4;

        while ($num < $num_uploads) {
            echo '<div><input name="ufile[]" type="file" id="ufile[]" size="50" /></div>';
            $num++;
        }
        /////////////////rest of whatever

And foreach through in controller, easy

Please or to participate in this conversation.