CI如何上传图片?

  |   php codeigniter

我打算用CodeIgniter写的上传文件功能,然后用postman测试上传文件,不过遇到了有问题就是不能上传,我想了好久就提出问题,一段时间还没得到解决好了。我非常怀疑自己忘了写的东西,尝试加上设置Body里面的输入file,还有加上php控制器代码里面的file,竟然成功上传了文件!

PHP文件:

public function upload_post()
{
    $this->load->helper(array('form', 'url'));

    $config = array(
        'upload_path' => "./uploads/",
        'allowed_types' => "gif|jpg|png|jpeg|pdf",
        'overwrite' => TRUE,
        'max_size' => "2048000",
        'max_height' => "768",
        'max_width' => "1024"
    );

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

    if($this->upload->do_upload())
    {
        $data = array('upload_data' => $this->upload->data());
        $this->set_response($data, REST_Controller::HTTP_CREATED);
    }
    else
    {
        $error = array('error' => $this->upload->display_errors());
        $this->response($error, REST_Controller::HTTP_BAD_REQUEST);
    }

}

HTML文件:

<?php echo $error;?>

<?php echo form_open_multipart('api/example/upload');?>

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

<br /><br />

<input type="submit" value="upload" />

</form>

测试浏览器就OK!

测试postman失败

然后修改代码和postman设置:

这样解决好了,原来是没有添加$this->upload->do_upload('file')key:file