1

I'm trying to do a file upload through ajax and php. The PHP works fine when called directly, but each time I call it through ajax it is failing. I'm not getting any errors (annoying) it just does not want to upload.

My JQUERY looks like

$('.fileUpload').on('click', function(){
        var file_data = $('#medical').prop('files')[0]; 
        console.log(file_data);  
            var form_data = new FormData();                  
            form_data.file = file_data;
            console.log(form_data); 
            var fileType = $(this).parent().find('input[type="hidden"]').val()
            console.log(fileType);                       
            $.ajax({
                        url: '/docs/upload.php', // point to server-side PHP script 
                        dataType: 'text',  // what to expect back from the PHP script, if anything
                        cache: false,
                        fileType:fileType,
                        contentType: false,
                        processData: false,
                        data: form_data,                         
                        type: 'post',
                        success: function(data){
                            $('.message').html(data) // display response from the PHP script, if any
                        }
             });
    });

and my PHP looks like

$file_upload="true";
$file_up_size=$_FILES['file_up'][size];
print_r($_FILES[file_up]);
if ($_FILES[file_up][size]>250000){$msg=$msg."Your uploaded file size is more than 250KB
 so please reduce the file size and then upload.<BR>";
$file_upload="false";}

$file_name=$_FILES[file_up][name];
$add="medicalPaperwork/$file_name"; // the path with the file name where the file will be stored

if($file_upload=="true"){

if(move_uploaded_file ($_FILES[file_up][tmp_name], $add)){
echo "Thank god!";
}else{echo "Fuck you.";}

}else{
echo $msg;
}

What am I doing wrong? I'm going crazy trying to figure this out.

edit: the content of the form_data enter image description here

zazvorniki
  • 3,512
  • 21
  • 74
  • 122

1 Answers1

0

You are using FormData incorrectly, use append to set the fields you want to upload

$('.fileUpload').on('click', function(){
    var file_data = $('#medical').prop('files')[0]; 
    console.log(file_data);  
    var form_data = new FormData();                  
    var fileType = $(this).parent().find('input[type="hidden"]').val()
    form_data.append('file_up', file_data);
    form_data.append('fileType', fileType);
    console.log(form_data); 
    console.log(fileType);                       
    $.ajax({
                url: '/docs/upload.php', // point to server-side PHP script 
                dataType: 'text',  // what to expect back from the PHP script, if anything
                cache: false,
                contentType: false,
                processData: false,
                data: form_data,                         
                type: 'post',
                success: function(data){
                    $('.message').html(data) // display response from the PHP script, if any
                }
     });
});
Musa
  • 96,336
  • 17
  • 118
  • 137