Php 带有图像上传的codeigniter表单

Php 带有图像上传的codeigniter表单,php,jquery,forms,codeigniter,upload,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['m

我的表单有一个字段(上传个人图片)。因此,用户从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);
    }
}
和控件编辑事件,如下所示:

  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);
    }
视图:


唐加尔
纳玛阿卡拉
伊西·凯贾坦
试试这个

    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);
    }
试试这个

    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);
    }

你能再帮我一次吗?你们可以在下面看到,我已经编辑了,我把控制器编辑放在了kegiatan。我尝试编辑但错误未定义变量:nama_kegiatan。怎么样?你能给我你的视图的代码吗?那是用来编辑的吗?什么是当前的?url()我想你没有在url中传递$id\u kegiatan看看,你的$id值不应该是空的:)你能再帮我一次吗?你们可以在下面看到,我已经编辑了,我把控制器编辑放在了kegiatan。我尝试编辑但错误未定义变量:nama_kegiatan。怎么样?你能给我你的视图的代码吗?当前的是什么?url()我想你没有在url中传递$id\u kegiatan看看,你的$id值不应该是空的:)