0

I'm working on an Application in the Laravel 5 framework which requires multiple images to be uploaded. For this, I have a form which with a file input (I researched and came across dropzone.js so I'm using this like this)

<form method="POST" enctype="multipart/form-data">
   <div class="form-group">
      <label for="inputAccName">Name:</label>
      <input type="text" name="inputAccName" class="form-control" id="inputAccName" required="true" value="{{ Auth::user()->name }}">
      <input type="hidden" name="_token" value="{{ csrf_token() }}">
   </div>
   <div class="dropzone" id="dropzoneFileUpload">
   </div>
   <script type="text/javascript">
      var baseUrl = "{{ url('/') }}";
      var token = "{{ Session::getToken() }}";
      Dropzone.autoDiscover = false;
       var myDropzone = new Dropzone("div#dropzoneFileUpload", { 
           url: baseUrl+"/user/uploadFiles",
           params: {
              _token: token
            }
       });
       Dropzone.options.myAwesomeDropzone = {
          paramName: "file", // The name that will be used to transfer the file
          maxFilesize: 2, // MB
          addRemoveLinks: true,
          accept: function(file, done) {

          },
        };
   </script>
   <input class="btn btn-success btn-lg" type="submit" name="formPassword" value="Save Changes">
</form>

So the user will input the name and upload up to 4 pictures (minimum 1 required).

I have a POST route which calls a function called "UpdateUserX" this function updates the user name in the database when the form is submitted.

In a separate function I have added the following code for the files upload:

public function uploadFiles()
{

    $destinationPath = 'uploads'; // upload path
    $extension = Input::file('file')->getClientOriginalExtension(); // getting file extension
    $fileName = rand(11111, 99999) . '.' . $extension; // renaming image
    $upload_success = Input::file('file')->move($destinationPath, $fileName); // uploading file to given path

    if ($upload_success) {
        return Response::json('success', 200); 

    } else {
        return Response::json('error', 400);
    }

}

This function is called by dropzone when images are uploaded. What I want to do from here is parse the details of uploaded images to the UpdateUserX function so i can store the details of the images in the database. Any advice in the right direction would be appreciated.

5
  • Updated my question - I got the uploading to work but what I want to do from here is parse the details of uploaded files to the UpdateUserX function which is called when I submit the form with the user details so I can store the details of the images in the database also. Commented Sep 23, 2015 at 14:34
  • Deleting my answer since you changed the question. I don't know the answer to your new question. Commented Sep 23, 2015 at 14:45
  • You have to store some information about the uploaded images in your uploadFiles method, so you can identify them later. Commented Sep 23, 2015 at 15:07
  • @peaceman I'm going to try this with sessions. Commented Sep 23, 2015 at 15:09
  • I wasn't able to do this with Dropzone.JS as I couldn't implement it to use an existing form. That way, it could have uploaded through the form and i could have captured it in my controller function. Instead, I have used a default input with the file type. If anyone has tips on how I can upload on form submit with Dropzone or the following plugin: github.com/kartik-v/bootstrap-fileinput it would be appreciated if you can put me in the right direction. Commented Sep 23, 2015 at 16:32

1 Answer 1

2

Incase anyone is looking to do something similar. I wasn't able to do this with Dropzone.JS as I couldn't implement it to use an existing form. That way, it could have uploaded through the form and i could have captured it in my controller function. Instead, I have used a default input with the file type:

<input id="file" type="file" name="file[]" multiple="true">

Then i simply capture in my UpdateUserX function:

$files = Input::file('file');
$file_count = count($files);
// start count how many uploaded
$uploadcount = 0;
foreach($files as $file) {
  $rules = array('file' => 'required'); //'required|mimes:png,gif,jpeg,txt,pdf,doc'
  $validator = Validator::make(array('file'=> $file), $rules);
  if($validator->passes()){
    $destinationPath = 'uploads';
    $filename = $file->getClientOriginalName();
    $upload_success = $file->move($destinationPath, $filename);
    $uploadcount ++;
  }
}

I will update my answer if in the future i find a way to do this with Dropzone or with the Bootstrap file input plugin: https://github.com/kartik-v/bootstrap-fileinput

UPDATE

I found the following plugin which styles the file input: http://markusslima.github.io/bootstrap-filestyle/#Getstart I am now using this instead of Dropzone.JS with the above code.

Via JavaScript:

$(":file").filestyle({input: false});

Via data attributes:

<input type="file" class="filestyle" data-input="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.