0

I have created file uploading function using ajax and codeigniter the problem is that when I am trying to upload an image it is working fine but when I try to upload document or excel file it is giving me null can anybody help me out to fix this issue

here is what error I am getting Array ( [error] => The filetype you are attempting to upload is not allowed.)

Here is my controller

public function post_forum() {
    $config['upload_path'] = './assets/uploads/';
    $config['allowed_types'] = 'gif|jpg|png|doc|docx|xls|xlsx|pdf';
    $this->load->library('upload', $config);

    if(!empty($_FILES['post_image']['name'])) {
         if (!$this->upload->do_upload('post_image')) {
             $error = array('error' => $this->upload->display_errors());
         } else {
             $dt = $this->upload->data();
             $file = $dt['file_name'];
         }
    } else {
        $file = '';
    }

    $word_dta    = $this->input->post('post_dt');
    $time_posted = time();
    $user_id     = $this->basic_model->is_login($this);
    empty($this->input->post('group_id')) ? $group_id   = '' : $group_id   = $this->input->post('group_id');
    empty($this->input->post('c_id'))     ? $company_id = '' : $company_id = $this->input->post('c_id');

    $data_upload = array(
        'upload_document' => $file,
        'discussion'      => $word_dta,
        'user_id'         => $user_id,
        'group_id'        => $group_id,
        'company_id'      => $company_id,
        'time_posted'     => $time_posted,
        'status'          => 'Posted'
    );

     $post_id = $this->basic_model->insertRecord($data_upload, 'forums');

$data = array(
         'file_name'   => $file,
         'data'        => $word_dta,
         'time_posted' => $time_posted,
         'post_id'     => $post_id,
         'name'        => $this->basic_model->getUserData(),
         'command'     => 'Submit Post'
     );

     $this->load->view('forum/includes/get_data', $data);
  }

This is my html form which I have created in views folder

<form method="POST" action="" id="postform" enctype="multipart/form-data" onsubmit="return false;">
                <input type="file" id="imageFieldId" name="post_image" />
                <div id="contentbox" contenteditable="true" name="post_dt">
                </div>
                <input type="submit" id="sb_art" class="btn_v2" value="Start Discussion" />
            </form>

This is my ajax call

$(document).ready(function(e) {
$("#postform").on('submit', (function(e) {
    $("#load").show();
    var form = this;
    var formData = new FormData(this);
    formData.append('post_dt', $("#contentbox").html());
    $.ajax({
        url         : "http://tfsquare.com/demo/forums/post_forum",
        type        : "POST",
        data        : formData,
        contentType : false,
        cache       : false,
        processData : false,
        success     : function(data) {
            $("#data_update").prepend($(data).fadeIn('slow'));
            $("#contentbox").empty();
            $('.no_st').css('display', 'none');
            form.reset();
        },
        complete: function() {
            $("#load").hide();
        }
    });
}));
});
Mark Alan
  • 435
  • 1
  • 5
  • 19

1 Answers1

1
$config['allowed_types'] = 'gif|jpg|png|doc|docx|xls|xlsx|pdf';

Codeigniter validate files not only with their extension but also with their MIME type. Probably Your given excel files doesn't have the mime entries in your Mimes.php which is located in config.php. If you can find the content type add add it to the mime array in mimes.php. If not manually validate the extension by exploding the file name and Do not use codeigniter's 'allowed_types'. Try this

 if(!empty($_FILES['post_image']['name'])) {
         if (!$this->upload->do_upload('post_image')) {
             $error = array('error' => $this->upload->display_errors());
         } else {
             $ext = ['xls', 'xlsx'];
             $dt = $this->upload->data();
             $file = $dt['file_name'];
             if( in_array($dt['file_ext'], $ext){
                echo 'Valid File';
              }


         }
    } else {
        $file = '';
    }
Giri Annamalai M
  • 810
  • 9
  • 24
  • I tried your code this is the error i am getting Array ( [error] => The filetype you are attempting to upload is not allowed. ) – Mark Alan Mar 29 '17 at 10:35