dounaidu0204 2017-03-11 16:36
浏览 33

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

I don't know where I got wrong but I couldn't find it out.

I have a form validation with a callback file check validation function

My Add_Post Function:

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

Callback function:

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

Here's for the view part:

<?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(); ?>

My Ajax code:

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

Input FileName Checked: I already checked the input name and it's the same.

Image Size: I checked for some related question of mine which is undefined index on file validation and they said image size must be big but I use a small size image sample.

I used Krajee's File Input Plugin.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)
    • ¥15 keil里为什么main.c定义的函数在it.c调用不了
    • ¥50 切换TabTip键盘的输入法
    • ¥15 可否在不同线程中调用封装数据库操作的类
    • ¥15 微带串馈天线阵列每个阵元宽度计算
    • ¥15 keil的map文件中Image component sizes各项意思