This post is about CodeIgniter(C.I.) file upload. By the use of file uploading class, you can easily upload a file or an image.
One can easily validate and restrict file type, its size and can even provide various preferences while uploading a file or image.
Using CodeIgniter Image Upload example, we will teach you how an image or a file gets uploaded to an upload folder or a targeted location.
Below is our complete code with live demo and download code option.
Watch the live demo or download code from the link given below
Note: Download the file and extract it to the root file of your local server.
Firstly, you’ll need to create a destination folder where you want your images to get uploaded.
So, Create a folder named “uploads” at the root of your CodeIgniter(CI) installation.
Now create two “view” files file_view.php and upload_success.php in a views folder of CodeIgniter . First file will display a complete form that contains upload field and the second file is to display the message “successfully uploaded”.
In your controller folder create a file naming: upload_controller.php
where we will going to load a library to initialize an Upload class using the below code:
$this->load->library('upload');
Also you have to set preferences for image and file i.e. size, type etc. based on that you can allow uploading. We have set file preferences in a function do_upload() of controller which looks like:
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
Now, you can also try it at your end by using similar URL
localhost/ci_demo/index.php/upload_controller/file_view
and pasting the complete code as shown below in your “views” and “controller” files.
Note: File uploads require a multipart form so we have include a form helper in controller to create the opening form tag as it create proper syntax you can also go for simple HTML form tags.
Syntax for form helper: –
$this->load->helper(array('form', 'url'));
Tutorial Scripts in detail
Below are the details of the code used in this tutorial with proper explanation.
View file for viewing complete form : file_view.php
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo $error;?> <!-- Error Message will show up here -->
<?php echo form_open_multipart('upload_controller/do_upload');?>
<?php echo "<input type='file' name='userfile' size='20' />"; ?>
<?php echo "<input type='submit' name='submit' value='upload' /> ";?>
<?php echo "</form>"?>
</body>
</html>
Controller file : upload_controller.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Upload_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function file_view(){
$this->load->view('file_view', array('error' => ' ' ));
}
public function do_upload(){
$config = array(
'upload_path' => "./uploads/",
'allowed_types' => "gif|jpg|png|jpeg|pdf",
'overwrite' => TRUE,
'max_size' => "2048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
'max_height' => "768",
'max_width' => "1024"
);
$this->load->library('upload', $config);
if($this->upload->do_upload())
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success',$data);
}
else
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('file_view', $error);
}
}
}
?>
Note : You can change preferences depending upon the file you wish to upload.
'allowed_types' => "gif|jpg|jpeg|png|iso|dmg|zip|rar|doc|docx|xls|xlsx|ppt|pptx|csv|ods|odt|odp|pdf|rtf|sxc|sxi|txt|exe|avi|mpeg|mp3|mp4|3gp",
View file to display success message : upload_success.php
<html>
<head>
<title>Upload File Specification</title>
</head>
<body>
<h3>Your file was successfully uploaded!</h3>
<!-- Uploaded file specification will show up here -->
<ul>
<?php foreach ($upload_data as $item => $value):?>
<li><?php echo $item;?>: <?php echo $value;?></li>
<?php endforeach; ?>
</ul>
<p><?php echo anchor('upload_controller/file_view', 'Upload Another File!'); ?></p>
</body>
</html>
Conclusion:
So in this tutorial we have focused on one of the topic of codeigniter which might have beneficial for you to understand it’s complete functionality. Keep reading our blog posts for getting in touch with more coding tricks.
39 Replies to “CodeIgniter Image and File Upload”
hey, It’s giving me error page not found
Hello Sohil,
Actually you might be using wrong path for calling controller class,
use this : localhost/ci_demo/index.php/Upload_Controller/file_view
Hope that helped you.
Thanks & Regards,
FormGet Team.
Join Us On
Facebook : https://www.facebook.com/FormGet
Twitter : https://twitter.com/FormGetcom
Google Plus : https://plus.google.com/+Formget
you should provide it with model admin, your website really complete but give it with insert database, put model in it 😀
Thanks
The model file is not needed in this task as there is not any database connectivity.
Thanks for reading the blog post.:)
Regards,
FormGet Team
Hey dear, so how to make validasion if the is input type name, address, zip, gender and upload file, how to implement validation in my form, if for input type i can make validation but to include validasion image and this i can’t do it?
if you have time please make some tutorial about it, Thanks
[email protected]
Hello Freddy,
For applying validation in form fields follow up this tutorial guide :
https://www.formget.com/form-validation-using-codeigniter/
And validations are already applied for the image or file to upload.
You can change it from the code itself :
$config = array(
‘upload_path’ => “./uploads/”,
‘allowed_types’ => “gif|jpg|png|jpeg|pdf”,
‘overwrite’ => TRUE,
‘max_size’ => “2048000”, // Can be set to particular file size , here it is 2 MB(2048 Kb)
‘max_height’ => “768”,
‘max_width’ => “1024”
);
Hope that will help you out.
Regards,
FormGet Team.
why it doesn’t work after download it, even open http://localhost/ci_demo/ file not found ?
just want to testing my form, ugh
Use this path for calling controller class :
localhost/ci_demo/index.php/upload_controller/file_view
Regards,
FormGet Team.
Not Found
The requested URL /uploading/index.php/upload_controller/file_view was not found on this server.
when iam executing the above error was coming to me. Will You help me.
How about make some input type name, there are name, age and upload foto, please give it with validation too, if you have time because it help people who will learn CI more ?
Please 😀
thank you
will you please tell me how to upload pptx file or can u please tell me mime array of pptx
Hi than you for your i tried it but it show me error like this please give me suggestion
Unable to select the specified database: college
Filename: E:\wamp\www\image\image\system\database\DB_driver.php
Line Number: 140
Check the error number it is a database error
Using this code image upload is succussfull but this code is also allowed upload different type of file ex: exe , is also upload is code
How can I insert image link in database?
where should the uploads folder be created exactly? in the application folder of codeigniter or along with the application folder in the projects folder???
i want upload image with content and title… and as well save in database too
and then how can reterive that image with title and content on the view page..
please kindly help me how i can do this ?????
Hi Sir,
I want to upload more than two files in folder with dynamic names,i.e there are two input type=”file”,how can i upload two files in folder with dynamic name.
Thanks and Regards:
Anurag Gupta
‘max_size’ => “2048000”, // Can be set to particular file size , here it is 2 MB(2048 Kb)
Check that line…must be “2048” only not “2048000”.
I want to update image by id, can I change the value of input type=file? If possible teachme
thanks you for tutorial but it has one error after uploading why ?
thanks you for tutorial but it has one error after uploading why ?
$this->load->library(‘upload’, $config);
if($this->upload->do_upload())
{
$data = array(‘upload_data’ => $this->upload->data());
$this->load->view(‘upload_success’,$data);
}
else
{
$error = array(‘error’ => $this->upload->display_errors());
$this->load->view(‘file_view’, $error);
}
}
}
i have confusion where “upload” is used. is it controller ?
actually upload is an array of image
write this code in your controllrer
$config[‘upload_path’] = ‘./uploads/post_images/images’;
$config[‘allowed_types’] = ‘gif|jpg|png’;
$config[‘max_size’] = ‘100’;
$config[‘max_width’] = ‘1024’;
$config[‘max_height’] = ‘768’;
$this->load->library(‘upload’, $config);
if ( ! $this->upload->do_upload())
{
$error = array(‘error’ => $this->upload->display_errors());
}
else
{
$upload_array = $this->upload->data();
//echo “”;
//print_r($upload_array);
//exit;
$name = $upload_array[“file_name”];
$path = $upload_array[“full_path”];
}
Its very useful
The upload path does not appear to be valid.
after putting the code..it shows this msg..
i create a folder i.e upload in ci>upload
would you please suggest?
$config = array(
‘upload_path’ => “./assets/uploads/”,
‘allowed_types’ => “gif|jpg|png|jpeg|pdf”,
‘overwrite’ => TRUE,
‘max_size’ => “2048000”, // Can be set to particular file size , here it is 2 MB(2048 Kb)
‘max_height’ => “768”,
‘max_width’ => “1024”
);
$this->load->library(‘upload’, $config);
Actually where i should create this upload.Is it a folder to save images or something else.While running the above code i am not getting the success message.please help me.
The upload path does not appear to be valid.
like that……
$this->upload->initialize($config); //added line
“$this->load->library(‘upload’, $config)”; //existing
I want this with having database…..It would be helpful for everybody…!!!!
“The upload path does not appear to be valid”
if this error found then add this line in to your controller above this line
“$this->load->library(‘upload’, $config)”;
like that……
$this->upload->initialize($config); //added line
“$this->load->library(‘upload’, $config)”; //existing
please also provide me to display (fetch) of image code…
Array ( [error] =>
You did not select a file to upload.
)
its Giving me a error “The upload path does not appear to be valid” ..
‘upload_path’ => “./images/”
Error was encountered
Severity: error
Message: call to undefined function form_open_multipart()
Filename: views/file_view.php
Line number:7
Any help please?
what is the function to get the Original file name, to read it ?
Can you please share how to upload an image along with form?