如何使用codeigniter-restserver上传文件?

时间:2018-01-27 14:11:14

标签: php codeigniter

我尝试使用Postman上传文件,但收到错误消息:You did not select a file to upload.我不知道是什么原因,但也试图找到方法。

看看邮递员的GIF:

enter image description here

看一下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>

使用浏览器上传文件即可!

enter image description here

两个代码没问题!浏览器上传没问题!但无法上传Postman!

已解决,原件未添加$this->upload->do_upload('file')和密钥file邮递员。

enter image description here

1 个答案:

答案 0 :(得分:0)

你可以把你的表格代码?我想你忘了添加enctype='multipart/form-data'