doutou7286 2016-07-05 06:51
浏览 43

未定义的索引错误(Codeigniter)

My code was working fine before. But Now it is not working. I am working on codeigniter and I am uploading a file using jquery ajax. I donot know why my code stop working. If you can find the issue please let me know.

Here is the controller code

public function updatedp()
{   
    $var = $_FILES['fileUp'];
    $img=$_FILES['fileUp'];
    $config['upload_path'] = 'webim/dp_images'; 
    $config['overwrite'] = 'TRUE';
    $config["allowed_types"] = 'jpg|jpeg|png|gif';
    $config["max_size"] = '1400';
    $config["max_width"] = '1400';
    $config["max_height"] = '1400'; 
    $this->load->library('upload', $config);

    if(!$this->upload->do_upload('fileUp')) 
    {               
        $this->data['error'] = $this->upload->display_errors(); 
        echo json_encode(array("result"=>$this->data['error']));
        exit;
    } else { 
        $data=array('active'=>0);
        $this->db->where('userid','1');
        $this->db->update('music_user_dp',$data);

        $uname['uname'] =$this->session->all_userdata('uname');
        $uname['id'] =$this->session->all_userdata('id');
        $post_data = array(
            'id' => '',
            'userid' => $uname['id']['id'], 
            'profilepic'=>$var['name'], 
            'updatedate' => date("Y-m-d H:i:s"),
            'active' => '1'
        );
        $this->Userpage_model->insert_dp_to_db($post_data);
        echo json_encode(array("result"=>"Success"));
        exit;
    }
}

My jquery code which calling above function:

$("#btnupdate").click(function(event){
 if($("#fileupload2").val() != ''){
    if (typeof FormData !== 'undefined') {
      var form = $('#formname').get(0); 
      var formData = new FormData(form);  

    $.ajax({
      type: "POST",
      url: "Userpage/updatedp",
      data: formData,
      mimeType:"multipart/form-data",
      dataType: 'json',
      xhr: function() {
         return $.ajaxSettings.xhr();
      },
      cache:false,                    
      contentType: false,
      processData: false,
      success: function(result){


       toastr8.info({
         message:'Profile Picture Updated', 
         title:"New Image Uploaded",
         iconClass: "fa fa-info",
       });
      }                       
    });
     event.preventDefault();
        }  

  } else {
     toastr8.info({
       message:'Error Occured', 
       title:"Please try again",
       iconClass: "fa fa-info",
    });
  }  
});

HTML:

<div class="modal fade" id="myModal" role="dialog">
<form enctype="multipart/form-data" name="formname" id="formname"  method="post" action="">
  <div class="modal-dialog">     
    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header "> 
        <script type="text/javascript">  
        $(document).ready(function(){
          $('#mgupload-dp').click(function(e){ 
            $('#fileupload2').click(); 
            e.preventDefault();
          });
        });
        </script>
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Create profile picture</h4>
      </div>
      <div class="modal-body">
        <div class="text-center" style="width:100%"> <img src="<?php echo base_url(); ?>img/profile.png" alt="add dp" id="pop-dp" >
          <button type="button" class="btn btn-default text-center" id="mgupload-dp">Choose picture to upload</button>
          <input type="file" id="fileupload2" name="fileUp" class="hidden-dp" accept="image/*">
        </div>
        <div class="clearfix"></div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" id="btnupdate">Update Picture</button>
      </div>
    </div>
  </div>
  </div>
</form>
</div>

Files are not uploading and I am getting this error

A PHP Error was encountered Severity: Notice Message: Undefined index: fileUp Filename: controllers/Userpage.php

  • 写回答

1条回答 默认 最新

  • douxin5953 2016-07-05 07:15
    关注
    1. Open the Firebug in FF.
    2. Click on the ajax call URL under Console.
    3. See what are passed under "Post" tab.
    4. If fileUp is present there, use $this->input->post('fileUp'); to fetch the contents.
    评论

报告相同问题?