Codeigniter文件上传验证未定义索引

时间:2017-03-11 16:36:51

标签: php jquery codeigniter

我不知道我错在哪里,但我找不到它。

我有一个带有回调文件检查验证功能的表单验证

我的Add_Post功能:

public function add_post()
{
    $validation = array (
                    array(
                        'field' => 'post_title',
                        'label' => 'Post title',
                        'rules' => 'trim|required|alpha_numeric'
                    ),
                    array(
                        'field' => 'headerimage',
                        'rules' => 'required|callback_file_check'
                    ),
                    array(
                        'field' => 'post_desc',
                        'label' => 'Post Description',
                        'rules' => 'trim|required|alpha_numeric'
                    ),
                    array(
                        'field' => 'post_content',
                        'label' => 'Post content',
                        'rules' =>  'trim|required'
                    )
                );

    $this->form_validation->set_rules($validation);

    if($this->form_validation->run()===FALSE)
    {
        $info['errors'] = validation_errors();
        $info['success'] = false;
    }
    else
    {
        $config = array(
                    "upload_path"  => "./uploads/blog_images",
                    "max_size"     => "2048000",
                    "allowed_types"=> "gif|png|jpg|jpeg",
                    "file_name"    => 'header_'.substr(md5(rand()),0,7),
                    "overwrite"    => false
                );  

        $this->load->library('upload', $config);

        if($this->upload->do_upload('file'))
        {
            $uploadFile = $this->upload->upload_data();
            $uploadFileName = $uploadFile['file_name'];
            $data = array(
                    "post_title" => $this->input->post('post_title'),
                    "post_content" => $this->input->post('post_content'),
                    "post_image" => $uploadFileName,
                    "post_created" => $this->input->post('time')
                );
            $this->Blog_model->add_post($data);
        }
        $info['success'] = true;
        $info['message'] = "Successfully added blog post";
    }
    $this->output->set_content_type('application/json')->set_output(json_encode($info));
}

回调功能:

public function file_check($str){
    $allowed_mime_type_arr = array('application/pdf', 'image/gif', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/x-png');
    $mime = get_mime_by_extension($_FILES['headerimage']['name']);
    if(isset($_FILES['headerimage']['name']) && $_FILES['headerimage']['name'] != "") {
        if(in_array($mime, $allowed_mime_type_arr)) {
            return true;
        } else {
            $this->form_validation->set_message('file_check', 'Please select only pdf/gif/jpg/png file.');
            return false;
        }
    } else {
        $this->form_validation->set_message('file_check', 'Please choose a file to upload.');
        return false;
    }
}

以下是视图部分:

<?php echo form_open_multipart('Blog/file_check',array("class"=>"form-horizontal","id"=>"blogform")); ?>
    <div class="row">
        <div class="col-md-6">
            <input type="text" placeholder="Enter your post title" name="post_title" class="form-control">
        </div>
        <div class="col-md-6 form-group">
            <label for="file" class="control-label col-md-4">Select header image:</label>
            <div class="col-md-8">
                <input type="file" name="headerimage" id="file" accept="image/*" />
            </div>
        </div>
    </div>
    <input type="text" placeholder="Enter your post description" name="post_desc" class="form-control">

    <label class="control-label text-muted">Post Body:</label>
    <div>
        <textarea id="post_content" name="content"></textarea>
    </div>
    <button class="btn btn-default pull-right" type="button" id="save-post"><i class="fa fa-save"></i> Save Post</button>
    <div class="clearfix"></div>
<?php echo form_close(); ?>

我的Ajax代码:

$(document).on('click','#save-post',function(){
    $post_content = $('#post_content').summernote('code');
    $time = $('#time').text();
    $.ajax({
        url:site_url('Blog/add_post'),
        data: $('#blogform').serialize() + "&post_content=" + $post_content + "&time=" + $time,
        type: "POST",
        dataType: 'json',
        encode: true,
        success: function(data){
            if(!data.success) {
                if(data.errors) {
                    $('#blog-message').html(data.errors).addClass('alert alert-danger');
                }
            } else {
                alert(data.message);
            }
        }
    });
});

输入FileName已选中:我已经检查了输入名称并且它是相同的。

图像尺寸:我检查了一些与我相关的问题,这是文件验证时未定义的索引,他们说图像尺寸必须很大,但我使用的是小尺寸图像样本。

我使用了Krajee的文件输入插件。

0 个答案:

没有答案