Codeigniter照片上传和使用


Codeigniter photo upload and use

我做了一个照片上传功能,它无法上传到根目录中的上传文件夹$this->upload->do_upload('userfile')总是不起作用,因为它总是转到Material_view页面。

查看器:

<?php echo form_open_multipart ('index.php/Main/do_upload'); ?>
<table class="table">
    <tr>
        <td>Title</td>
        <td><?php echo form_input('title'); ?></td>
    </tr>
    <tr>
        <td>Image</td>
        <td><?php echo form_upload('userfile'); ?></td>
    </tr>
    <tr>
        <td></td>
        <td><?php echo form_submit('submit', 'Save', 'class="btn btn-primary"'); ?></td>
        <?php echo form_close(); ?>
    </tr>       
</table>

控制器

public function do_upload()
    {
            $config['upload_path']          = './uploads/';
            $config['allowed_types']        = 'gif|jpg|png';
            $config['max_size']             = 100;
            $config['max_width']            = 1024;
            $config['max_height']           = 768;
            $this->load->library('upload', $config);
            if ( ! $this->upload->do_upload('userfile'))
            {
                    $error = array('error' => $this->upload->display_errors());
                    $this->load->view('Material_view', $error);
            }
            else
            {
                    $data = array('upload_data' => $this->upload->data());
                    $this->load->view('upload_success', $data);
            }
    }

我可以问一下,在我上传照片后,我如何在查看器中使用它吗?非常感谢:)

我已经根据您的新代码更改了答案:

您调用了一个不正确的字段(因为您没有修改教程的示例以适合您的自定义视图。

控制器:

public function do_upload()
{
        $config['upload_path']          = './uploads/';
        $config['allowed_types']        = 'gif|jpg|png';
        $config['max_size']             = 100;
        $config['max_width']            = 1024;
        $config['max_height']           = 768;
        $this->load->library('upload', $config);
        if ( ! $this->upload->do_upload('pic'))
        {
                $error = array('error' => $this->upload->display_errors());
                $this->load->view('Material_view', $error);
        }
        else
        {
                $data = array('upload_data' => $this->upload->data());
                $this->load->view('upload_success', $data);
        }
}