Search code examples
javascriptphpjqueryajaxmultifile-uploader

No file being uploaded while uploading multiple files using AJAX


I am trying to upload multiple files with only 1 AJAX request. But I am facing some problems and they are:

UPDATE

  • No file is being uploaded to the server
  • It seems to me that multiple AJAX requests are being made as AJAX is inside the for loop.

How to sort these problems?

HTML

<!--<form id="fileupload" method="POST" enctype="multipart/form-data">-->
    <input type="file" multiple name="uploadfile[]" id="uploadfile" />
<!--</form>-->

JS

$("#uploadfile").change(function(){
     //submit the form here  
    //$('#fileupload').submit();
    var files = $("#uploadfile")[0].files;
    for (var i = 0; i < files.length; i++){
        //alert(files[i].name);
        var data = files[i].name;
        $.ajax({
            type:'POST',
            url: 'mupld.php',
            data: data
        });
    }

//var files = $('#uploadfile').prop("files"); //files will be a FileList object.
//alert(files);
//var names = $.map(files, function(val) { return val.name; }); //names is an array of strings (file names)

});

PHP

<?php
if(isset($_FILES['uploadfile'])){

    $errors= array();

    foreach($_FILES['uploadfile']['tmp_name'] as $key => $tmp_name ){
        $file_name = $key.$_FILES['uploadfile']['name'][$key];
        $file_size =$_FILES['uploadfile']['size'][$key];
        $file_tmp =$_FILES['uploadfile']['tmp_name'][$key];
        $file_type=$_FILES['uploadfile']['type'][$key]; 
        if($file_size > 2097152){
            $errors[]='File size must be less than 2 MB';
        }       

        //$query="INSERT into upload_data (`USER_ID`,`FILE_NAME`,`FILE_SIZE`,`FILE_TYPE`) VALUES('$user_id','$file_name','$file_size','$file_type'); ";

        $desired_dir="storage";

        if(empty($errors)==true){
            if(is_dir($desired_dir)==false){
                mkdir("$desired_dir", 0700);        // Create directory if it does not exist
            }

            if(is_dir("$desired_dir/".$file_name)==false){
                move_uploaded_file($file_tmp,"$desired_dir/".$file_name);
            }
            else{                                   // rename the file if another one exist
                $new_dir="$desired_dir/".$file_name.time();
                 rename($file_tmp,$new_dir) ;               
            }
         //mysql_query($query);         
        }
        else{
                print_r($errors);
        }
    }
    if(empty($error)){
        echo "Success";
    }
}
?>

Solution

  • js at Question does not appear to upload File object, but File.name at var data = files[i].name; ? <!-- --> is not valid comment in javascript

    Try using FormData()

    $("#uploadfile").change(function() {
        var files = $("#fileupload")[0];
            $.ajax({
                type:"POST",
                url: "mupld.php",
                data: new FormData(files),
                processData: false,
                contentType: false
            });
    });