Codeigniter and RestServer. How to upload images?

I am coding an API using Phils RestServer (see link) in Codeigniter. I need a way to upload images via API. How can i do this? It's as simple as making a POST request with the correct headers (which headers to use)?

https://github.com/philsturgeon/codeigniter-restserver

Thanks for entering!

+7
source share
5 answers

Good,

Here is another solution: Downloading FIL from the leisure client to the leisure server

But none of these solutions worked for me.

However, this is what worked; actually worked.

Firstly, I was not sure if my file reached the method, so I changed the response line to:

function enter_post() { $this->response($_FILES); } 

Please note that this is a great way to test your REST methods.

You can also output:

$ this-> answer ($ _ SERVER);

and

$ this-> answer ($ _ POST);

and etc.

I got the following JSON output:

{"file": {"name": "camel.jpg", "type": "application / octet stream", "tmp_name": "/ TMP / phpVy8ple", "error": 0, "size": 102838 }}

So, I knew that my file was there.

Then I changed the method to find and move the file. I used file sharing script to get the file from its temporary location and move it to a new location:

  $uploaddir = '/home/me/public_html/uploads/'; $uploadfile = $uploaddir . basename($_FILES['file']['name']); if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) { $data['status'] = 'uploaded'; } else { $data['status'] = 'failed'; } 
+9
source

In fact, you can use the native CodeIgniter File Upload Class to make uploading easier with Phil Rest-Server as follows:

 /** * REST API Upload using native CI Upload class. * @param userfile - multipart/form-data file * @param submit - must be non-null value. * @return upload data array || error string */ function upload_post(){ if( ! $this->post('submit')) { $this->response(NULL, 400); } $this->load->library('upload'); if ( ! $this->upload->do_upload() ) { $this->response(array('error' => strip_tags($this->upload->display_errors())), 404); } else { $upload = $this->upload->data(); $this->response($upload, 200); } } 
+5
source

try this code, the image will be saved with a timestamp and its extension ..

 $uploaddir = 'uploads/'; $path = $_FILES['image_param']['name']; $ext = pathinfo($path, PATHINFO_EXTENSION); $user_img = time() . rand() . '.' . $ext; $uploadfile = $uploaddir . $user_img; if ($_FILES["image_param"]["name"]) { if (move_uploaded_file($_FILES["image_param"]["tmp_name"],$uploadfile)) { echo "success"; } 
+2
source

Considering how amazon s3 does it here http://www.anyexample.com/programming/php/uploading_files_to_amazon_s3_with_rest_api.xml

It looks like on the client side you determine the type of file and then load it into a variable using file_get_contents($file_path) and then send it with the appropriate headers defined in the example.

Then on the server, I assume that it will use file_put_contents() with the request body to save the file.

0
source

Below is the configuration of the image path and their corresponding attributes set in the Form.php controller in the form () function

  $config['upload_path'] = 'uploads/images/full'; $config['allowed_types'] = 'gif|jpg|png'; $config['max_width'] = '6000'; $config['max_height'] = '6000'; $config['encrypt_name'] = true; $this->load->library('upload', $config); $this->upload->initialize($config); $uploaded = $this->upload->do_upload($imgfile); if($uploaded) 

{$ filenames = $ this-> fullimage_upload1 ($ this-> upload-> data ($ imgfile)); return $ filenames; }

This is a function that defines the loading of a copy of an image in different folders as small, thumbnails function fullimage_upload1 ($ data) {$ This-> load-> library ('image_lib');

  //this is the larger image $config['image_library'] = 'gd2'; $config['source_image'] = 'uploads/images/full/'.$data['file_name']; $config['new_image'] = 'uploads/images/small/'.$data['file_name']; $config['maintain_ratio'] = TRUE; /*$config['width'] = 600; $config['height'] = 500;*/ $this->image_lib->initialize($config); $this->image_lib->resize(); $this->image_lib->clear(); //cropped thumbnail $config['image_library'] = 'gd2'; $config['source_image'] = 'uploads/images/full/'.$data['file_name']; $config['new_image'] = 'uploads/images/thumbnails/'.$data['file_name']; $config['maintain_ratio'] = TRUE; $config['width'] = 300; $config['height'] = 258; $this->image_lib->initialize($config); $this->image_lib->resize(); $this->image_lib->clear(); return $data['file_name']; } 
0
source

All Articles