December 27, 2019
How to upload a file using Codeigniter Rest API
In this tutorial we are going to see how to upload image using rest API in PHP codeigniter.
In API developement we need a case to upload image or file.Below example gives basic idea of uploading image using Codeigniter
Example for upload image using rest API in PHP codeigniter
View File :
1 2 3 4 5 6 7 8 9 10 11 |
<html> <head> <title>File Upload In Codeigniter</title> </head> <body> <?php echo form_open_multipart('upload_controller/do_upload');?> <?php echo "input type='file' name='logo' size='20' />"; ?> <?php echo "input type='submit' name='submit' value='upload' /> ";?> <?php echo "</form>"?> </body> </html> |
Controller File Uploading image using Codeigniter :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
public function do_upload(){ $postData = $this->post(); $config = array( 'upload_path' => "img/logo", //path for upload 'allowed_types' => "gif|jpg|png|jpeg", //restrict extension 'max_size' => '100', 'max_width' => '1024', 'max_height' => '768', 'file_name' => 'logo_'.date('ymdhis') ); $this->load->library('upload',$config); if($this->upload->do_upload('logo')) { $data = array('upload_data' => $this->upload->data()); $path = $config['upload_path'].'/'.$data['upload_data']['orig_name']; // Write query to store image details of login user { } $returndata = array('status'=>0,'data'=>'user details','message'=>'image uploaded successfully'); $this->set_response($returndata, 200); } else { $error = array('error' => $this->upload->display_errors()); $returndata = array('status'=>0,'data'=>$error,'message'=>'image upload failed'); $this->set_response($returndata, 200); } } |
Happy Coding…
