Formdata file does not work when loading file in codelgniter, Getting error

All I am trying to do is upload files using ajax to my CodeIgniter based site. But I cannot get the value of the file field in the controller. I get a message like "Undefined index: 'file_1'"

How to solve this problem?

The form

<form method="post" enctype="multipart/form-data">
 <input type="file" id="file_1" name="file_1" value="" class="field1" /> 
 <input type="button" onclick="up_img()" value="Upload" /> 



<script type="text/javascript">
function up_img()
   formdata = false;
    var imgfile = document.getElementById("file_1"); 
    formdata = new FormData(); 
        url: "<?php echo base_url(); ?>index.php/save_project_upload/",  
        type: 'POST',   
        data: formdata,
        processData: false,
        contentType: false,
        success: function (data) {  



 function save_project_upload()

echo $upfile_name           =       $_FILES['file_1']['name'];




source to share

1 answer

If you do print_r( $_FILES )

, do you get any content?

Also, keep in mind that this is not supported by IE8 or earlier.

You can also try:

var formData = new FormData($('#yourformID')[0]);     



formData.append('file_1', $('input[id="file_1"]')[0].files[0]); 


You can also solve your problem by adding [0]

to the DOM element.



All Articles