Upload File In Codeigniter

Let’s start to write some code that uploads your files on the server.

Step 1:

Make a simple form in the views.

<form action="index.php/welcome/do_upload" method="post" enctype="multipart/form-data">
	<input type="file" name="userFile"/>
	<input type="submit"/>
</form>

Step 2 :

In controller just make a method like this to upload your files. And don’t forget to make a folder outside the application folder to move your files.

public function do_upload(){
		$config['upload_path'] = './uploads';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_width'] = "1024";
		$config['max_height'] = '786';
		
		$this->load->library('upload',$config);
		
		if( !$this->upload->do_upload('userFile') ){
			$error = array( 'error' => $this->upload->display_errors() );
		}
		else{
			$data = array( 'upload_data' => $this->upload->data() );
		}
		
}

That’s all you need to do to upload your files. Don’t forget to comment if you learn some thing from us.