donglu8549 2013-11-24 18:43
浏览 54
已采纳

Codeigniter上传文件不起作用

function submit_article() {
    $this->load->helper(array('form', 'url'));
    $this->load->library('form_validation');
    $this->form_validation->set_error_delimiters('<p style="color:red">', '<br/></p>');

    $my_rules = array(
        array(
            'field' => 'title',
            'label' => 'Title',
            'rules' => 'required|min_length[5]|max_length[20]|xss_clean'
        ),
        array(
            'field' => 'additionalUpload',
            'label' => 'Additional Upload',
            'rules' => 'callback_is_image'
        )
    );

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

    if ($this->form_validation->run() == FALSE) {
        //ERROR
        $data['title'] = ucfirst('submit Article');
        $this->load->view('templates/header', $data);
        $this->load->view('submit_article', $data);
        $this->load->view('templates/footer', $data);
    } else {
        //SUCCESS
        $data['title'] = ucfirst('article Submitted');
        $this->load->view('templates/header', $data);
        $this->load->view('forms_view/submit_article_success', $data);
        $this->load->view('templates/footer', $data);
    }
}

function is_image($value) {

    $config['upload_path'] = './public/uploads/';
    $config['allowed_types'] = 'gif|jpg|png|pdf|tiff';
    $config['max_size'] = '2048';
    $config['max_width'] = '0';
    $config['max_height'] = '0';
    $config['remove_spaces'] = true;

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

    if (!$this->upload->do_upload()) {
        $this->form_validation->set_message('is_image', $this->upload->display_errors('<p style="color:red">', '<br/></p>'));
        return FALSE;
    } else {
        $this->upload->data();
        return TRUE;
    }
}

Hi every one, this is my controller function code for processing multipart form data in codeigniter, actually the field additionalUpload is not a required feild, but I want it to be validated if the user upload file in additionalUpload field of file type, when I run the above code and click on submit button without selecting any file its shows me error "You did not select a file to upload." which I do not want because this is not a required field, this is my first problem..

and second one is that when I select a file and click on submit button it again show me that "You did not select a file to upload.".

Note: I have just shown two fields of my form here that is title,additionalUpload but I have all total 9 fields.

THANKS IN ADVANCE PLEASE HELP ANYONE.

  • 写回答

2条回答 默认 最新

  • douhao6557 2013-11-24 19:00
    关注

    The first thing you have to put the name of the file field.

    <input type="file" name="image"/>
    $this->upload->do_upload('image');
    

    Second, you can not have the max_width and max height 0

    $config['max_width']    = '2048';
    $config['max_height']   = '2048';
    

    Try that first and then see

    For validate field file:

    if($_FILES['you_field_name']['tmp_name']){
    
           //your code  
    }
    

    A greeting

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波