Php只上传$ _files数组中的最后一个文件

时间:2017-08-31 23:01:27

标签: javascript php jquery ajax

我有一个PHP脚本,可以处理通过ajax脚本推送给它的文件的服务器端上传; ajax脚本实际上通过文件输入循环并将文件推送到服务器:print_r($ _ FILES);一组文件,但只有一个文件最终被上传到服务器。这是我上传文件的PHP脚本;

$i=0;

foreach($_FILES as $file_item){
    $i++;
    //print_r($_FILES['file']['name'][$i]);

    echo  $file_item['tmp_name'][$i];

    $sourcePath = $file_item['tmp_name'][$i];
    $targetPath = "uploads/".basename($file_item['name'][$i]); //. round(microtime(true)) . '.' . $file_item['tmp_name'][$i];
    echo $targetPath;
    move_uploaded_file($sourcePath, $targetPath);

}

这是我前端的ajax脚本;

$(function(){
    $('#file').change(function(){
    for (i=0; i < this.files.length; i++){
        console.log(this,'this');
        console.log(this.files[i]);
        var file=this.files[i];     
        var imagetype=file.type;
        var imgtags =['image/jpeg','image/pjpeg',"image/png","image/jpg","image/gif"];      
        if(imgtags.indexOf(imagetype)>0){
            Materialize.toast('File Is an image!', 2000);           
            var filereader=new FileReader();
            filereader.onload=FileLoadCheck;
            filereader.readAsDataURL(this.files[i]);
            FileUploadAjaxCall();
        }
            }
    });
    function FileLoadCheck(e){
        console.log(e,'Object');
        $('#previewImage').attr('src',e.target.result);
    }

});

function FileUploadAjaxCall(){  
    $.ajax({
        url:'fileupload.php?_'+new Date().getTime(),
            type:'POST',
            data:new FormData($('#UploadMedia').get(0)),
            contentType:false,
            cache:false,
            processData:false,
            success:function(data){
                console.log(data,'data');
                Materialize.toast('File Upload Successfully!', 2000);
            }
    });
}

这是html

<form method="post" enctype="multipart/form-data" id="UploadMedia">
    <div class="container-fluid">
        <div class="row">               
            <div class="col s6 push-s3">
                <div class="card card-panel hoverable">
                    <div class="card-image">
                        <img src="images/images.png" class="img-responsive" id="previewImage">
                        <span class="card-title">Image Show Here</span>
                    </div>
                    <div class="card-content">
                        <div class="file-field input-field">
                            <div class="btn" style="background-color:#757575">
                                <span>File</span>
                                <input type="file" name="file[]" id="file" multiple>
                            </div>
                            <div class="file-path-wrapper">
                                <input class="file-path validate" type="text" placeholder="Upload one or more files">
                            </div>
                        </div>                          
                    </div>                                                      
                </div>
            </div>                              
        </div>              
    </div>  
</form>
  

我已经检查了类似的问题和php.ini max_upload限制已经没有运气了

0 个答案:

没有答案