1

i am unable to upload image using dropzone.js in php.

i have tried everything but nothing work for me.

MY HTML Code

<form method="post" enctype="multipart/form-data">
    <div class="dropzone" id="mydropzone">
        <input type="file" name="img">
        <div class="dropzone-previews"></div>
    </div>
    <button type="submit" id="submitbtn">Upload</button>
</form>

MY Javascript Code

   Dropzone.options.mydropzone = {
                url:"upload.php",
                uploadMultiple: true,
                maxFilesize: 99,
                maxFiles : 3,
                autoDiscover:false,
                acceptedFiles: ".png,.jpg,.jpeg",
                previewsContainer: '.dropzone-previews',
                autoProcessQueue : false,
                parallelUploads: 100,
                addRemoveLinks: true,       
                init:function(){
                    var myDropzone = this;
                    $("#submitbtn").on('click',function(e) {
                       e.preventDefault();
                       myDropzone.processQueue();
                    });
                }

           }

MY PHP Code (I Tried it work 100% fine but with dropzone.js not work) $upload_dir = "img/";

$name = $_FILES['img']['name'];
$tmp_file = $_FILES['img']['tmp_name'];

move_uploaded_file($tmp_file, $upload_dir.$name);
2
  • What does "not working" mean? Error messages? Have you checked the browsers console to see if you get any JS errors? Have you checked the network tab in your browsers dev tools to see what actually gets passed and what the response is? Commented Aug 26, 2019 at 10:11
  • DropzoneJs + PHP: How to build a file upload form Commented Aug 26, 2019 at 10:16

1 Answer 1

2

You don't need to add input type file in your html

<form method="post" enctype="multipart/form-data">
<div class="dropzone" id="mydropzone">
    <div class="dropzone-previews"></div>
</div>
<button type="submit" id="submitbtn">Upload</button>

Try this Code. Hope this will work

 var fileList = new Array;
 var i = 0;
 var myDropzone = new Dropzone("div#mydropzone", { 
     url:"upload.php",
     uploadMultiple: true,
     maxFilesize: 99,
     maxFiles : 3,
     autoDiscover:false,
     acceptedFiles: ".png,.jpg,.jpeg",
     previewsContainer: '.dropzone-previews',
     autoProcessQueue : false,
     parallelUploads: 100,
     addRemoveLinks: true,       
     init:function(){
        this.on("success", function (index, response) {
            var res = JSON.parse(response);
            console.log(res);
            fileList = res.images;
            for (i = 0; i < fileList.length; i++) {
              var imgname = fileList[i];
              $(".dz-remove").eq(index).attr('data-url',imgname);
           }
        $('.dz-success-mark').show();
        });
     }
 });

In you upload.php file return response with image name

  $name = $_FILES['img']['name'];
$tmp_file = $_FILES['img']['tmp_name'];
$filesCount = count($_FILES[$img]['name']);
for($i = 0; $i < $filesCount; $i++) { 
   move_uploaded_file($tmp_file[$i], $upload_dir.$name[$i]);
   $upload_image[] = $name[$i];
}
$response_data=array('images'=>$upload_image);
echo json_encode($response_data);   
Sign up to request clarification or add additional context in comments.

2 Comments

It will not work. Console error: Uncaught Error: Dropzone already attached.
Try html code without form tag` <div class="form-group col-md-12"> <div class="dropzone dz-square" id="mydropzone"></div> </div>`

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.