CodeIgniter上传图片没有输入

时间:2016-07-29 01:52:52

标签: php codeigniter

我正在尝试实现CodeIgniter的upload库。但每当我尝试上传时,它都会继续说我没有选择要上传的文件。

以下是代码:

查看:

<form action="<?= base_url().'principal/updateuniform' ?>" method="post">
                    <h3 style="text-align:center">Polo</h3>
                    <input type="hidden" name="imageid" value="<?= $img_id ?>">
                    <input type="hidden" name="imagename" value="<?= $img_name ?>">
                    <input type="file" name="imagefile" accept="image/*" /><br>
                    <button type="submit" class="btn btn-gold"><span class="glyphicon glyphicon-upload" aria-hidden="true"></span> Upload</button>
                </form> 

上传功能:

function updateuniform(){
        $img_id = $this->input->post('imageid');
        $img_name = $this->input->post('imagename');

        $config['upload_path']          = './images/';
        $config['allowed_types']        = 'gif|jpg|png';
        $config['max_size']             = 5000;
        $config['max_width']            = 3000;
        $config['max_height']           = 4000;
        $config['overwrite']            = TRUE;
        $config['file_name']            = $img_name;

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

        $img = $this->input->post('imagefile');;

        $id = $this->session->userdata('id');
        $user['user'] = $this->UsersModel->select_principal($id);

        if (!$this->upload->do_upload($img)){
            $data['error'] = $this->upload->display_errors();
            $data['uniforms'] = $this->InfoModel->getUniforms();

            $this->load->view('include/header_principal',$user);
            $this->load->view('principal/manage_uniforms', $data);
            $this->load->view('include/footer_principal');
        } else {
            $img_path = 'images/'.$this->upload->data('orig_name');
            $data['success'] = 'Uniforms has been updated.';

            $this->InfoModel->updateUniform($img_path);

            $this->load->view('include/header_principal',$user);
            $this->load->view('principal/manage_uniforms', $data);
            $this->load->view('include/footer_principal');
        }

    }

我还尝试echo $img并显示文件名。

2 个答案:

答案 0 :(得分:1)

$this->upload->do_upload($img)

param应该是域名,如:

$this->upload->do_upload('imagefile')

答案 1 :(得分:0)

在您的表单标记中添加enctype =“multipart / form-data”,这样您就可以获得$ _FILE值,这将解决您的问题我想