mercredi 23 septembre 2015

PHP Upload multiple image files in Laravel 5 failing

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">
   <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+"/uploads",
           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. Within the same function, I have added the following code for the files:

    $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); 
        //add details for the images to the database 

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

I'm lost as to where to go from here on. When I submit the form, it seems the files don't upload correctly as I get the following error:

Call to a member function getClientOriginalExtension() on a non-object



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire