7

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

github.com/philsturgeon/codeigniter-restserver

Now, I am using Codeignitor rest client :

github.com/philsturgeon/codeigniter-restclient

to get/post data from/to the rest server and am successfully able to get/post normal data.

What is the best way to post image files to the rest server from the rest client ?

Also, how can someone can access the API and perform all get/post operation from C# .NET assuming the rest server uses digest authentication ? [any library available ?]

slayton
  • 20,123
  • 10
  • 60
  • 89
Amit Aggarwal
  • 131
  • 1
  • 2
  • 8
  • 3
    okay, PhilSturgeon suggested me a way to do this ... send file_get_contents(ImageFile) from client as POST variable with say name 'imageFile' to server and on server side create the image file via file_put_contents($_POST['imageFile']) ... but now issue is when I receive it on server, some characters (like ÿØÿ) are stripped and image is not created perfectly. To overcome this stripping problem, I encoded the image data in base64 format on client side before sending and then decoded it back on server side and it worked perfectly. Any other workaround ? Is this the best approach ? – Amit Aggarwal Dec 29 '10 at 12:46
  • 1
    You should post this process as the answer. It really is the best solution. – Noah Goodrich Nov 30 '11 at 20:06

5 Answers5

5

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);
}
Srivaths
  • 121
  • 1
  • 3
1

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);
      }
    }
G.Ashok Kumar
  • 1,649
  • 2
  • 13
  • 25
  • I tried this code but getting the error Array ( [error] => You did not select a file to upload. ) Please let me know how to set the post parameters and headers – Cecil Paul Jan 06 '18 at 06:15
0

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.

Phil Sturgeon
  • 30,637
  • 12
  • 78
  • 117
0

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

Community
  • 1
  • 1
sekati
  • 535
  • 5
  • 12
0

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

Codeigniter and RestServer. How to upload images?

Community
  • 1
  • 1
ssaltman
  • 3,623
  • 1
  • 18
  • 21