Codeigniter - 文件上传

时间:2017-02-12 16:24:42

标签: php codeigniter

我在控制器中有这个

public function add() {
        $usertype = $this->session->userdata("usertype");
    $this->data['usertype'] = $usertype;
    if($usertype == "Student") {
      $this->data['users'] = $this->teacher_m->get_order_by_teacher();
    } else {
      $this->data['users'] = $this->siteusers_m->get_site();
    }

        if( $usertype !="Parent") {
            if($_POST) {
                $rules = $this->rules();
                $this->data['set'] = $this->input->post("to");
                $this->form_validation->set_rules($rules);
                if ($this->form_validation->run() == FALSE) {
                    $this->data['form_validation'] = validation_errors();
                    $this->data["subview"] = "leave/add";
                    $this->load->view('_layout_main', $this->data);
                } else {
                    $array = array(
                        "title" => $this->input->post("title"),
                        "details" => $this->input->post("details"),
                        "fdate" => $this->input->post("fdate"),
                        //"photo" => $this->input->post("image"),
                        "tousername" => $this->input->post("to"),
                        "fromusername" => $this->session->userdata('username')
                    );
                            if($_FILES["attachment"]['name'] !="") {
                    $file_name = $_FILES["attachment"]['name'];
                    $file_name_rename = rand(1, 100000000000);
                    $explode = explode('.', $file_name);
                    if(count($explode) >= 2) {
                        $new_file = $file_name_rename.'.'.$explode[1];
                        if (preg_match('/\s/',$file_name)) {
                            $file_name = str_replace(' ', '_', $file_name);
                        }
                        $config['upload_path'] = "./uploads/attach";
                        $config['allowed_types'] = "gif|jpg|png|pdf|docx|csv";
                        $config['file_name'] = $new_file;
                        $config['max_size'] = '1024';
                        $config['max_width'] = '3000';
                        $config['max_height'] = '3000';
                        $array['attach'] = $file_name;
                        $array['attach_file_name'] = $new_file;
                        $this->load->library('upload', $config);
                        if(!$this->upload->do_upload("attachment")) {
                            $this->data["attachment_error"] = $this->upload->display_errors();
                            $this->data["subview"] = "leave/add";
                            $this->load->view('_layout_main', $this->data);
                        } else {
                            $data = array("upload_data" => $this->upload->data());
                            $this->leaveapp_m->insert_leave($array);
                            $this->session->set_flashdata('success', $this->lang->line("menu_success"));
                            redirect(base_url("leave/index"));
                        }
                    } else {
                        $this->data["attachment_error"] = "Invalid file";
                        $this->data["subview"] = "leave/add";
                        $this->load->view('_layout_main', $this->data);
                    }
                } else {

                    $this->leaveapp_m->insert_leave($array);
                    $this->session->set_flashdata('success', $this->lang->line("menu_success"));
                    redirect(base_url("leave/index"));
                }
            }
        } else {
            $this->data["subview"] = "leave/add";
            $this->load->view('_layout_main', $this->data);
        }
    }
    }

一旦点击提交,保存所有细节而不上传附件文件。试了几个小时没有成功。

比我尝试删除此行

{

                $this->leaveapp_m->insert_leave($array);
                $this->session->set_flashdata('success', $this->lang->line("menu_success"));
                redirect(base_url("leave/index"));
            }

比给我一个错误消息:未定义的索引:附件...... 请帮忙不知道如何解决这个问题..

这是我的观点

<div class="form-group">
                    <div class="btn btn-info btn-file">
                      <i class="fa fa-paperclip"></i> <?=$this->lang->line('attachment')?>
                      <input type="file" id="attachment" name="attachment"/>
                    </div>
                    <div class="col-sm-3" style="padding-left:0;">
                        <input class="form-control"  id="uploadFile" placeholder="Choose File" disabled />
                    </div>
                    <div class="has-error">
                        <p class="text-danger"> <?php if(isset($attachment_error)) echo $attachment_error; ?></p>
                    </div>
                  </div>

和脚本

    <script type="text/javascript">
//$('#fdate').datepicker();
//$('#tdate').datepicker();
document.getElementById("attachment").onchange = function() {
document.getElementById("uploadFile").value = this.value;
};
$('#leave').jqte();
</script>

0 个答案:

没有答案