使用var_dump将文件名称设置为null

时间:2017-03-27 06:18:23

标签: php ajax codeigniter

我已经使用ajax和codeigniter创建了文件上传功能,问题是当我尝试上传图片时工作正常但是当我尝试上传文件或excel文件时它给了我null我可以帮助我解决这个问题

这是我得到的错误数组([error] =>不允许您尝试上传的文件类型。)

这是我的控制器

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);
  }

这是我在视图文件夹

中创建的html表单
<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>

这是我的ajax电话

$(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();
        }
    });
}));
});

1 个答案:

答案 0 :(得分:1)

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

Codeigniter不仅验证文件的扩展名,还验证其MIME类型。可能你的给定excel文件中没有mimes.php中的mime条目,它位于config.php中。 如果您可以找到内容类型,则将其添加到mimes.php中的mime数组中。如果没有通过爆炸文件名手动验证扩展名,请不要使用codeigniter的'allowed_types'。 试试这个

 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 = '';
    }