如何在 php 中使用 dropzone.js 上传任何文件?

how to upload any file using dropzone.js in php?

我无法在 php 中使用 dropzone.js 上传图片。

我已经尝试了所有方法,但对我没有任何作用。

我的HTML代码

<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>

我的Javascript代码

   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();
                    });
                }

           }

我的 PHP 代码(我试过它 100% 正常但 dropzone.js 不工作) $upload_dir = "img/";

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

move_uploaded_file($tmp_file, $upload_dir.$name);

您不需要在 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>

试试这个代码。希望这会奏效

 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();
        });
     }
 });

在你的 upload.php 文件中 return 响应带有图片名称

  $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);