Skip to content

Uploading PDF files with CodeIgniter ?

Last updated on January 17, 2018

CodeIgniter is a PHP framework, CodeIgniter has a number of helpers and libraries, which will reduce the development time and we can write more reliable and bugs free Code. Here, This post is about uploading files in CodeIgniter, CodeIgniter has upload library, by using this Class we can upload files to server very easily

For Uploading files, as usually we need a Simple HTML form, with an input field and submit button.

Here is the Code:


 'file','name' => 'userfile'));
 echo form_submit('submit','upload'); 
 echo form_close(); 
 ?>

Now, in your Controller method, you need to set some config settings like uploading path, allowed types, upload size, width, height……ect..

function do_upload()
	{
		// load codeigniter helpers
		$this->load->helper(array('form','url'));
		// set path to store uploaded files
		$config['upload_path'] = './uploads/';
		// set allowed file types
		$config['allowed_types'] = 'pdf';
		// set upload limit, set 0 for no limit
		$config['max_size']	= 0;
 
		// load upload library with custom config settings
		$this->load->library('upload', $config);
 
		 // if upload failed , display errors
		if (!$this->upload->do_upload())
		{
			$this->data['error'] = $this->upload->display_errors();
		     $this->data['page_data'] = 'admin/upload_view';
		     $this->load->view('admin/admin', $this->data);
		 }
		else
		{
			  print_r($this->upload->data());
			 // print uploaded file data
		}
	}

if you need to upload large files, you need to increase Max file uploading limit in php.ini on your server or you can increase upload limit with htaccess.

if you see “The filetype you are attempting to upload is not allowed” error while uploading pdf files, just go to config/mines.php file open it and change the bellow line

// from
'pdf'	=>	array('application/pdf', 'application/x-download'),
// to 
'pdf' => array('application/pdf', 'application/x-pdf', 'application/x-download', 'binary/octet-stream', 'application/unknown', 'application/force-download'),
0 0 votes
Article Rating
Subscribe
Notify of
guest

9 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments