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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化