FIle upload from a rest client to a rest server

后端 未结 5 968
长发绾君心
长发绾君心 2021-02-09 07:01

I created a rest server using the codeigniter rest server library created by PhilSturgeon :

github.com/philsturgeon/codeigniter-restserver

Now, I am using Codei

相关标签:
5条回答
  • 2021-02-09 07:32

    Simple Rest Post service to upload & store a file from any Rest client

    public function product_post()
    {
        $uploaddir = './uploads/products/';
        $file_name = underscore($_FILES['file']['name']);
        $uploadfile = $uploaddir.$file_name;
    
        if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
            $dataDB['status'] = 'success';       
            $dataDB['filename'] = $file_name;
         } else {
            $dataDB['status'] =  'failure';       
         }
         $this->response($dataDB, 200);
    }
    
    0 讨论(0)
  • 2021-02-09 07:33

    After searched for more than 1 days, finally this is what worked for me

    public function upload_post()
        {
          $config['upload_path'] = './uploads/';
          $config['allowed_types'] = '*';
    
          $this->load->library('upload');
          $this->upload->initialize($config);
    
          if ( ! $this->upload->do_upload('file'))
          {
            $error = array('error' => $this->upload->display_errors());
             print_r($error);
          //  $this->load->view('upload_form', $error);
          }
          else
          {
            $data = array('upload_data' => $this->upload->data());
            print_r($data);
          //  $this->load->view('upload_success', $data);
          }
        }
    
    0 讨论(0)
  • 2021-02-09 07:34

    As Amit has suggested, you can send the contents of a file file_get_contents('foo.jpg') or you could do something more true to the spec and do something like:

    POST /images

    Content-Type: image/jpeg

    Then for the body you just set the contents of the image (or whatever type of file you're firing off).

    You can then pick that up in your PHP with:

    file_get_contents('php://input');
    

    That will have the contents of your image, so you can save it wherever.

    0 讨论(0)
  • 2021-02-09 07:47

    I was actually able to use CodeIgniters native File Upload class to solve this same issue: https://stackoverflow.com/a/11163068/1188523

    0 讨论(0)
  • 2021-02-09 07:48

    These solutions did not work for me. However, I posted my solution here:

    Codeigniter and RestServer. How to upload images?

    0 讨论(0)
提交回复
热议问题