CodeIgniter上传功能,有时不起作用

时间:2015-12-22 11:57:46

标签: php codeigniter image-uploading

我有codeigniter上传功能的问题。 该功能运行良好,但有时无法正常工作且没有任何错误信息。

控制器中的代码段

...
        function add_process() {
                $data['title'] = anchor('event/','<b>EVENT</b>', array('class' => 'back'));
                $data['subtitle'] = ' / Add Event';
                $data['main_view'] = 'event/event_form';
                $data['form_action'] = site_url('event/add_process');       

                $this->form_validation->set_rules('eventName', 'Event Name', 'required');
                $this->form_validation->set_rules('eventDate', 'Event Date', 'required');
                if (empty($_FILES['eventImage']['name'])){
                    $this->form_validation->set_rules('eventImage', 'Event Image', 'required');     
                }               

                if ($this->form_validation->run() == TRUE) {    
                    $config['upload_path'] = './images/event/';
                    $config['allowed_types'] = 'jpg|jpeg|png';          
                    //$config['max_width'] = '3000';
                    //$config['max_height'] = '3000';

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

                    $this->upload->do_upload('eventImage');
                    $eventImage = $this->upload->data();

                    $event = array( 'eventName'         => $this->input->post('eventName'),
                                    'eventDate'         => date('Y-m-d', strtotime($this->input->post('eventDate'))),
                                    'eventDescriptions' => $this->input->post('eventDescriptions'),
                                    'eventImage'        => 'images/event/'.$eventImage['file_name'],
                                    'isActive'          => $this->input->post('isActive')
                                );
                    $this->Event_model->add($event);            

                    $this->session->set_flashdata('message', '1 record was successfully added!');
                    redirect('event/add');      

                } else  {           
                    $this->load->view('admin/admin_main', $data);
                }
            } 
    ...
你能告诉我,我在这里失踪了什么?

1 个答案:

答案 0 :(得分:0)

替换此

$this->Event_model->add($event);            

$this->session->set_flashdata('message', '1 record was successfully added!');
redirect('event/add'); 

到这个

if ($this->Event_model->add($event)) {
    $this->session->set_flashdata('message', '1 record was successfully added!');
    redirect('event/add'); 
}