具有图像上传的codeigniter表单

时间:2014-05-01 04:54:36

标签: php jquery forms codeigniter upload

我有一个我的表格字段(上传个人照片)。因此用户从pc中选择图像并提交表单。

控制器:

function do_upload()
{
    $config['upload_path']   = './images/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size']      = '100000';
    $config['max_width']     = '10240';
    $config['max_height']    = '7680';

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

    if ( ! $this->upload->do_upload())
    {
        $error = array('error' => $this->upload->display_errors());

        $this->load->view('upload_form', $error);
    }
    else
    {
        $data = array('upload_data' => $this->upload->data());
        print_r($this->upload->data());
        $datafoto = $this->upload->data();
        $nm_file = time().$datafoto['orig_name'];
        $this->load->model('mkegiatan');
        $this->mkegiatan->update_foto($nm_file);
        copy('images/'.$datafoto['orig_name'], 'images/'.$nm_file);
    }
}

和contoller编辑这样的事件:

  function edit_kegiatan()
        {

            $id_kegiatan = $this->uri->segment(4);

            //set validation properties

            $this->form_validation->set_rules('tanggal_kegiatan', 'Tanggal', 'required');
            $this->form_validation->set_rules('nama_kegiatan', 'Judul Berita', 'required');
            $this->form_validation->set_rules('content', 'Content', 'required');


            //run validation
            // jika dia ingin update data atau form validation error
            if ($this->form_validation->run() == FALSE) {

                $data  = $this->mkegiatan->get_by_id($id_kegiatan);
                $this->load->model('mkegiatan');
                $config['upload_path'] = './images/';
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size'] = '100000';
                $config['max_width']  = '10240';
                $config['max_height']  = '7680';

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

                if ( ! $this->upload->do_upload('userfile'))       //you forgot this
                {
                    $error = array('error' => $this->upload->display_errors());

                    $this->load->view('upload_form', $error);
                }
                else
                {
                    $data = array('upload_data' => $this->upload->data());
                    //print_r($this->upload->data());
                    $datafoto=$this->upload->data();
                    $nm_file = time().$datafoto['orig_name'];
                   $data = array(
                        'tanggal_kegiatan' => $this->input->post('tanggal_kegiatan'),
                        'nama_kegiatan' => $this->input->post('nama_kegiatan'),
                        'content' => $this->input->post('content'),
                        'image' => $nm_file
                );
                $this->mkegiatan->update_kegiatan($id_kegiatan,$data);
                $this->session->set_flashdata('message', generateSuccessMessage('Data berhasil diupdate'));
                redirect(site_url('admin/kegiatan'));
                }
            }

            $this->data['orang'] = $this->mlogin->dataPengguna($this->session->userdata('username'));



            //var_dump($data);
            //$tmp_data = array('id_kegiatan' => $id_kegiatan);

            $this->data['contents'] = $this->load->view('admin/kegiatan/edit_kegiatan', $this->data, true);
            $this->load->view('template/wrapper/admin/wrapper_ukm',$this->data);
    }

查看:

<?php echo form_open(current_url(),'name=form'); ?>
                        <div class="fours fields">
                            <div class="field">
                                <div class="ui vertical segment">
                                    <div class="date field">
                                        <label>Tanggal</label>
                                        <div class="ui small icon input left">
                                            <input type="text" id="datepicker" placeholder="xxxx-xx-xx" name="tanggal_kegiatan" value="<?php echo $tanggal_kegiatan;?>"><?php echo form_error('tanggal_kegiatan', '<div class="ui red pointing label">', '</div>'); ?>
                                            <i class="calendar icon"></i>
                                          </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="two fields">
                            <div class="field">
                                <label>Nama Acara</label>
                                <div class="ui small left icon input">
                                    <input type="text" placeholder="Nama Kegiatan" name="nama_kegiatan" value="<?php echo $nama_kegiatan;?>"><?php echo form_error('nama_kegiatan', '<div class="ui red pointing label">', '</div>'); ?>
                                    <i class="text file outline icon"></i>
                                </div>
                            </div>
                        </div>

                        <div class="field">
                            <label>Isi Kegiatan</label>
                            <textarea placeholder="Text" name="content">
                                 <?php echo $content;?>
                            </textarea><?php echo form_error('content', '<div class="ui red pointing label">', '</div>'); ?>
                        </div>

                        <input type="file" name="userfile" size="20">

                    <input class="ui small blue submit button" type="submit" value="Save">
                    </form>

我是否需要创建两个单独的表单来完成此操作(一个用于图像上载,一个用于文本输入)?或者是否可以在控制器中编写一个可以同时验证和处理上传和文本输入的功能?

1 个答案:

答案 0 :(得分:6)

试试这个

    function tambah_kegiatan()
{
    //set validation properties
    $this->form_validation->set_rules('nama_kegiatan', 'Judul Berita', 'required');


    if ($this->form_validation->run() == true) 
    {
       $this->load->model('mkegiatan');
        $config['upload_path'] = './images/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '100000';
        $config['max_width']  = '10240';
        $config['max_height']  = '7680';

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

        if ( ! $this->upload->do_upload('userfile'))       //you forgot this
        {
            $error = array('error' => $this->upload->display_errors());

            $this->load->view('upload_form', $error);
        }
        else
        {
            $data = array('upload_data' => $this->upload->data());
            print_r($this->upload->data());
            $datafoto=$this->upload->data();
            $nm_file = time().$datafoto['orig_name'];
           $data = array(
                'nama_kegiatan' => $this->input->post('nama_kegiatan'),
                'image' => $nm_file
        );

        $this->mkegiatan->insert_kegiatan($data);
        $this->session->set_flashdata('message', generateSuccessMessage('Data berhasil ditambah'));
        redirect(site_url('admin/kegiatan'));
        }


    }

    $this->data['orang'] = $this->mlogin->dataPengguna($this->session->userdata('username'));
    $this->data['contents'] = $this->load->view('admin/kegiatan/tambah_kegiatan', '', true);
    $this->load->view('template/wrapper/admin/wrapper_ukm',$this->data);
}

并且您的图片文件夹具有所有权限

 function edit_kegiatan($id_kegiatan='')
        {



            //set validation properties

            $this->form_validation->set_rules('tanggal_kegiatan', 'Tanggal', 'required');
            $this->form_validation->set_rules('nama_kegiatan', 'Judul Berita', 'required');
            $this->form_validation->set_rules('content', 'Content', 'required');


            //run validation
            // jika dia ingin update data atau form validation error
            if ($this->form_validation->run() == FALSE) {

                $data  = $this->mkegiatan->get_by_id($id_kegiatan);
                $this->load->model('mkegiatan');
                $config['upload_path'] = './images/';
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size'] = '100000';
                $config['max_width']  = '10240';
                $config['max_height']  = '7680';

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

                if ( ! $this->upload->do_upload('userfile'))       //you forgot this
                {
                    $error = array('error' => $this->upload->display_errors());

                    $this->load->view('upload_form', $error);
                }
                else
                {
                    $data = array('upload_data' => $this->upload->data());
                    //print_r($this->upload->data());
                    $datafoto=$this->upload->data();
                    $nm_file = time().$datafoto['orig_name'];
                   $data = array(
                        'tanggal_kegiatan' => $this->input->post('tanggal_kegiatan'),
                        'nama_kegiatan' => $this->input->post('nama_kegiatan'),
                        'content' => $this->input->post('content'),
                        'image' => $nm_file
                );
                $this->mkegiatan->update_kegiatan($id_kegiatan,$data);
                $this->session->set_flashdata('message', generateSuccessMessage('Data berhasil diupdate'));
                redirect(site_url('admin/kegiatan'));
                }
            }

            $this->data['orang'] = $this->mlogin->dataPengguna($this->session->userdata('username'));



            //var_dump($data);
            //$tmp_data = array('id_kegiatan' => $id_kegiatan);

            $this->data['contents'] = $this->load->view('admin/kegiatan/edit_kegiatan', $this->data, true);
            $this->load->view('template/wrapper/admin/wrapper_ukm',$this->data);
    }