使用新数组的Php ajax多文件上传

使用新数组的Php ajax多文件上传,php,jquery,ajax,file-upload,Php,Jquery,Ajax,File Upload,其矿井上传html代码 <div class="col-xs-12"> <label for="upload" class="btn btn-sm btn-outline green btn-block"> <i class="fa fa-upload"></i> upload </label><input type="file" multiple id="upload" class

其矿井上传html代码

 <div class="col-xs-12">
    <label for="upload" class="btn btn-sm btn-outline green btn-block">
       <i  class="fa fa-upload"></i>
       upload
   </label><input type="file" multiple id="upload"  class="hidden"/>
 </div>
单击上载按钮时的Ajax代码

var myFormData = new FormData();
        uploadingFile.forEach(function (file) {
            myFormData.append('myFiles', file);
        });
        myFormData.append('a', 1);
        $.ajax({
            url: 'ajax/upload.php',
            type: 'POST',
            processData: false, // important
            contentType: false, // important
            data: myFormData,success: function(data, textStatus, jqXHR)
            {
              console.log(data);
            }
        });
最后一段代码是php代码

    $myFiles=$_FILES["myFiles"];

for($i=0; $i<count($myFiles); $i++){
    $target_path = "uploaded_files/";
    print_r($myFiles[$i]);
    echo $myFiles[$i]["tmp_name"]."\n";
    $ext = explode('.',$myFiles[$i]["tmp_name"]);
    $target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext)-1];

    $sonuc=move_uploaded_file($myFiles[$i], $target_path);
    if(move_uploaded_file($myFiles[$i], $target_path)) {
        echo "The file has been uploaded successfully \n";
    } else{
        echo "There was an error uploading the file, please try again! \n";
    }
}
$myFiles=$\u文件[“myFiles”];

对于($i=0;$i,当您获得空对象时,我认为您没有提交文件。我认为您没有在表单标记中使用enctype=“multipart/form data”。我认为如果您像下面这样将其添加到表单中,它会起作用


您使用这行代码
myFormData.append('myFiles',file);
附加单个文件。您必须使用这段代码
myFormData.append('myFiles[],file);
发送多个文件。下面是我实现的可以上载多个文件的代码

在脚本中,将其放置在代码下面

<script type="text/javascript">
    $("#upload").change(function (event) {
        var files = event.target.files;
        var uploadingFile = [];
        files = Object.values(files);
        files.forEach(function (file) {
            if (file.type.match('image')
                || file.type.match('application/vnd.ms-excel')
                || file.type.match('application/pdf')
                || file.type.match('application/vnd.openxmlformats-officedocument.wordprocessingml.document')
                || file.type.match('application/vnd.openxmlformats-officedocument.presentationml.presentation')
            ) {
                uploadingFile.push(file);
            }
        });

        var myFormData = new FormData();
        uploadingFile.forEach(function (file) {
            myFormData.append('myFiles[]', file);
        });

        $.ajax({
            url: 'upload.php',
            type: 'POST',
            processData: false, // important
            contentType: false, // important
            data: myFormData,success: function(data, textStatus, jqXHR)
            {
              console.log(data);
            }
        });
    });
</script>
$target_path = "uploaded_files/";

$myFiles=$_FILES["myFiles"];
if(count($myFiles['name']) > 1){
    $total = count($myFiles['name']);
    for($i=0; $i<$total; $i++){
        $ext = explode('.',$myFiles["name"][$i]);
        $target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext)-1];

        if(move_uploaded_file($myFiles["tmp_name"][$i], $target_path)) {
            echo "The file has been uploaded successfully \n";
        } else{
            echo "There was an error multiple uploading the file, please try again! \n";
        }
    }
} else {
    $ext = explode('.',$myFiles["name"]);
    $target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext)-1];

    if(move_uploaded_file($myFiles["tmp_name"], $target_path)) {
        echo "The file has been uploaded successfully \n";
    } else{
        echo "There was an error uploading the file, please try again! \n";
    }
}

$(“#上传”).change(函数(事件){
var files=event.target.files;
var uploadingFile=[];
文件=对象。值(文件);
forEach(函数(文件){
if(file.type.match('image'))
||file.type.match('application/vnd.ms excel')
||file.type.match('application/pdf')
||file.type.match('application/vnd.openxmlformats officedocument.wordprocessingml.document')
||file.type.match('application/vnd.openxmlformats of icedocument.presentationml.presentation')
) {
uploadingFile.push(文件);
}
});
var myFormData=new FormData();
uploadingFile.forEach(函数(文件){
append('myFiles[],file');
});
$.ajax({
url:'upload.php',
键入:“POST”,
processData:false,//重要
contentType:false,//重要
数据:myFormData,成功:函数(数据,文本状态,jqXHR)
{
控制台日志(数据);
}
});
});
在你上传的php代码中,把代码放在下面

<script type="text/javascript">
    $("#upload").change(function (event) {
        var files = event.target.files;
        var uploadingFile = [];
        files = Object.values(files);
        files.forEach(function (file) {
            if (file.type.match('image')
                || file.type.match('application/vnd.ms-excel')
                || file.type.match('application/pdf')
                || file.type.match('application/vnd.openxmlformats-officedocument.wordprocessingml.document')
                || file.type.match('application/vnd.openxmlformats-officedocument.presentationml.presentation')
            ) {
                uploadingFile.push(file);
            }
        });

        var myFormData = new FormData();
        uploadingFile.forEach(function (file) {
            myFormData.append('myFiles[]', file);
        });

        $.ajax({
            url: 'upload.php',
            type: 'POST',
            processData: false, // important
            contentType: false, // important
            data: myFormData,success: function(data, textStatus, jqXHR)
            {
              console.log(data);
            }
        });
    });
</script>
$target_path = "uploaded_files/";

$myFiles=$_FILES["myFiles"];
if(count($myFiles['name']) > 1){
    $total = count($myFiles['name']);
    for($i=0; $i<$total; $i++){
        $ext = explode('.',$myFiles["name"][$i]);
        $target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext)-1];

        if(move_uploaded_file($myFiles["tmp_name"][$i], $target_path)) {
            echo "The file has been uploaded successfully \n";
        } else{
            echo "There was an error multiple uploading the file, please try again! \n";
        }
    }
} else {
    $ext = explode('.',$myFiles["name"]);
    $target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext)-1];

    if(move_uploaded_file($myFiles["tmp_name"], $target_path)) {
        echo "The file has been uploaded successfully \n";
    } else{
        echo "There was an error uploading the file, please try again! \n";
    }
}
$target_path=“uploaded_files/”;
$myFiles=$\u文件[“myFiles”];
如果(计数($myFiles['name'])>1){
$total=count($myFiles['name']);
对于($i=0;$i)
试试这个


})

在ajax请求之前,使用
console.log
检查
myFormData
的值。您没有看到该操作使用ajax上载文件吗?
                $("#finalupload").uploadFile({
                       url: "badocumentsupload",
                       id: "test",
                       formData: {
                           action: 'uploadfile',
                           _token: tempcsrf,
                           baid:curaddbaid
                       },
                       fileName: "myfile",
                       returnType: "json",
                       showDelete: true,
                       showDone: false,
                       showProgress: true,
                       onSuccess: function (files, data, xhr) {

                       },
                       deleteCallback: function (data, pd) {
                           $.post("finaluploaddeletecustom", {
                                   action: "delete_current",
                                   row_id: data['DBID'],
                                   filetodelete: data['uploadedfile'],
                                   _token: tempcsrf
                               },
                               function (resp, textStatus, jqXHR) {
                                   reloaddatatable_final();
                               });

                           pd.statusbar.hide();//You choice to hide/not.
                       }
                   });