how to upload multiple files in one <input> - codeigniter

I have made a code that let me upload multiple files but in separate
I was trying to upload a multiple files in one input where i set my input tag into
<input type="file" multiple="" name="file1">
I selected 3 images but only 1 image was uploaded..
here is my VIEW before changing my input:
<?php echo form_open_multipart('test'); ?>
<p>
<?php echo form_label('File 1: ', 'file1') ?>
<input type='file' name='file1' id='file1'>
</p>
<p>
<?php //echo form_label('File 2: ', 'file2') ?>
<input type='file' name='file2' id='file2'>
</p>
<p><?php echo form_submit('submit', 'Upload them files!') ?></p>
and here is my CONTROLLER
function index()
{
if (isset($_POST['submit']))
{
$this->load->library('upload');
$config['upload_path'] = './upload_documents/';
$config['allowed_types'] = 'jpg|png|gif|jpeg|JPG|PNG|GIF|JPEG';
$config['max_size'] = '0'; // 0 = no file size limit
$config['max_width'] = '0';
$config['max_height'] = '0';
$config['overwrite'] = TRUE;
$this->upload->initialize($config);
foreach($_FILES as $field => $file)
{
// No problems with the file
if($file['error'] == 0)
{
// So lets upload
if ($this->upload->do_upload($field))
{
$data = $this->upload->data();
//alert("nice");
}
else
{
$errors = $this->upload->display_errors();
die();
}
}
else{
echo "alksjdfl";
die();
}
}
}
$this->load->view("test");
}
}

Use this multi file upload library
https://github.com/stvnthomas/CodeIgniter-Multi-Upload/blob/master/MY_Upload.php
// Prepraing upload config & upload files
$config = array();
$config['upload_path'] = 'temp/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '256000';
$this->load->library('upload');
$this->upload->initialize($config);
$this->upload->do_multi_upload("my_file");
$files_upload = $this->upload->get_multi_upload_data();
$err_msg = $this->upload->display_errors();
if (!empty($files_upload) && is_array($files_upload)) {
foreach ($files_upload as $file_data) {
// Your files here :)
}
}
Special thanks to #stvnthomas for the library :)

You should use this library for multi upload in CI
https://github.com/stvnthomas/CodeIgniter-Multi-Upload
Installation
Simply copy the MY_Upload.php file to your applications library directory.
Use: function test_up in controller
public function test_up(){
if($this->input->post('submit')){
$path = './public/test_upload/';
$this->load->library('upload');
$this->upload->initialize(array(
"upload_path"=>$path,
"allowed_types"=>"*"
));
if($this->upload->do_multi_upload("myfile")){
echo '<pre>';
print_r($this->upload->get_multi_upload_data());
echo '</pre>';
}
}else{
$this->load->view('test/upload_view');
}
}
upload_view.php in applications/view/test folder
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="myfile[]" id="myfile" multiple>
<input type="submit" name="submit" id="submit" value="submit"/>

Related

[Codeigniter]Multiple upload cant insert to database and not uploading to directory

How to upload multiple images with Codeigniter 4.x ?
I tried to loop the do_upload function in my model but it's not working, and I tried another code that I got from GitHub but still, none of it works.
This is my view:
<div class="section-body">
<h2 class="section-title">Entry Group Passanger</h2>
<div class="row">
<div class="col-md-12">
<form action="<?= base_url('passanger/addgroup') ?>" method="POST" enctype="multipart/form-data">
<?php for($i=1;$i<=$pax['pax'];$i++) : ?>
<h5>Passanger ke-<?= $i ?></h5>
<div class="form-row">
<div class="col-md-6">
<label for="fullname">Fullname</label>
<input type="text" class="form-control" name="name[]">
<?= form_error('name', '<small class="text-danger pl-3">', '</small>'); ?>
</div>
<div class="col-md-6">
<label for="gender">Gender</label>
<select class="form-control select2-input" name="gender">
<option>-- Select Gender --</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
<option value="Other">Other</option>
</select>
<?= form_error('gender', '<small class="text-danger pl-3">', '</small>'); ?>
</div>
</div>
<div class="form-row">
<div class="col-md-12 mb-4 mt-2">
<label for="">Upload Passport</label>
<input type="file" name="upload_passport[]" id="input-file-now" class="dropify" />
</div>
</div>
this is my model :
public function addGroup()
{
$user = $this->db->get_where('user', ['username' => $this->session->userdata('username')])->row_array();
$pax = $this->getPaxById();
date_default_timezone_set('Asia/Jakarta');
$upload_image = $_FILES['upload_passport[]']['name'];
if ($upload_image) {
$config['upload_path'] = './assets/img/uploads/passport';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048';
$this->load->library('upload', $config);
for($i=0;$i<$pax['pax'];$i++) {
if ($this->upload->do_upload('upload_passport[$i]')) {
$new_image = $this->upload->data('file_name');
} else {
echo $this->upload->display_errors();
}
}
}
$name = $this->input->post("name[]");
$gender = $this->input->post("gender[]");
$birthdate = $this->input->post("birthdate[]");
$no_passport = $this->input->post("no_passport[]");
$expire_passport = $this->input->post("expire_passport[]");
$destination = $this->input->post("destination[]");
$date = $this->input->post("date[]");
$dp1 = "0";
$dp2 = "0";
$lunas = "0";
$type = "Group";
$status = "Not Complete";
$permission = "0";
$upload_passport = $new_image;
$upload_dp1 = "default.png";
$upload_dp2 = "default.png";
$date_created = date('d/m/Y H:i:s');
$data = [];
$jumlah = $pax['pax'];
for($x=0;$x<$jumlah;$x++) {
array_push($data, [
'name'=>$this->input->post("name[$x]"),
'id_user'=> $user['id_user'],
'gender'=>$this->input->post("gender[$x]"),
'birthdate'=>$this->input->post("birthdate[$x]"),
'no_passport'=> $this->input->post("no_passport[$x]"),
'expire_passport'=> $this->input->post("expire_passport[$x]"),
'destination'=> $this->input->post("destination[$x]"),
'date'=> $this->input->post("date[$x]"),
'dp1'=> $dp1,
'dp2'=> $dp2,
'lunas'=> $lunas,
'pax'=> $pax['pax'],
'type'=> $type,
'status'=> $status,
'permission'=> $permission,
'upload_passport'=> $new_image,
'upload_dp1'=> $upload_dp1,
'upload_dp2'=> $upload_dp2,
'date_created'=> $date_created
]);
var_dump($data); die;
}
$this->db->insert_batch("passanger", $data);
}
What I want to get is:
array('','','upload_image'), array('','','upload_image')
When insert it to database but when I do it the upload just gets output null and it's not even uploaded to my uploads directory. I would really appreciate it if you help me, I've been stuck on this for 3 days. Thank you.
for ($i=0; $i < sizeof($_FILES['image']['name']) ; $i++) {
$_FILES['file']['name'] = $_FILES['image']['name'][$i];
$_FILES['file']['type'] = $_FILES['image']['type'][$i];
$_FILES['file']['tmp_name'] = $_FILES['image']['tmp_name'[$i];
$_FILES['file']['error'] = $_FILES['image']['error'][$i];
$_FILES['file']['size'] = $_FILES['image']['size'][$i];
$config['upload_path'] = PRODUCT_IMAGE_UPLOAD_PATH;
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
if ($this->upload->do_upload('file')){
$images[] = $this->upload->data('file_name');
}else {
echo $this->upload->display_errors();
}
}
Try this in Codeigniter 4
in your controller add this
$image_model = new App\Models\Images(); // Your model name that is load your model
if($imagefiles = $this->request->getFiles())
{
foreach($imagefiles['uploadImages'] as $img)
{
if ($img->isValid() && ! $img->hasMoved())
{
$image_name = $img->getRandomName(); // This is applicable if you want to change the name of the images
if($image_model->save_images($image_name))
{
$img->move(WRITEPATH.'uploads/images/ads', $image_name); // WRITEPATH.'uploads/images/ads' is the part you wish to save you file to
}
}
}
}
In your Model e.g Image model i have a method call save_images()as this
use Codeigniter\Model
class Images extends Model{
public function save_images($image = '')
{
// We set the column 'name' to be allow for data entry into the db. note this is mandatory for now.
// hope it will change in feature. 'name' is the column in database table where you will save the image name
// this command the CI4 db to select only the image column in the database table
$this->allowedFields = ['name'];
return $this->db->table('images')->insert(['name' => $image]);
// echo $sql = $this->db->getLastQuery();
}
}
Then in your html file input, do this
<form action="" method="post" enctype="multipart/form-data">
<div class="form-group">
<input type="file" name="uploadImages[]" multiple>
</div>
</form>
I hope this will work for you

upload multiple pictures in codeigniter

HTML Code
<?php echo form_open_multipart('upload/do_upload');?>
<input type="file" name="file1" size="20" />
<input type="file" name="file2" size="20" />
<input type="submit" value="upload" />
</form>
Controller code
public function do_upload()
{
$pic1 = "Name One";
$pic2 = "Name Two";
$RealName = array('file1', 'file2' );
$ChangeName = array($pic1, $pic2 );
$arrlength = count($ChangeName);
for($x = 0; $x < $arrlength; $x++)
{
$config['upload_path'] = './uploads/';
$config['file_name'] = $ChangeName[$x];
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = 1909900;
$this->load->library('upload', $config);
$this->upload->do_upload($RealName[$x]);
echo $ChangeName[$x]; echo "<br>";
echo $RealName[$x]; echo "<br>";
}
}
Trying to upload multiple pictures. Code runs correctly but I'm facing some problems in saving all pictures. The problem is saving all pictures with same name (Name One).
try this one out. this code is I am using...
private function upload_files($path, $title, $files)
{
$config = array(
'upload_path' => $path,
'allowed_types' => 'jpg|gif|png',
'overwrite' => 1,
);
$this->load->library('upload', $config);
$images = array();
foreach ($files['name'] as $key => $image) {
$_FILES['images[]']['name']= $files['name'][$key];
$_FILES['images[]']['type']= $files['type'][$key];
$_FILES['images[]']['tmp_name']= $files['tmp_name'][$key];
$_FILES['images[]']['error']= $files['error'][$key];
$_FILES['images[]']['size']= $files['size'][$key];
$fileName = $title .'_'. $image;
$images[] = $fileName;
$config['file_name'] = $fileName;
$this->upload->initialize($config);
if ($this->upload->do_upload('images[]')) {
$this->upload->data();
} else {
return false;
}
}
return $images;
}

Not displaying error when using upload multi file in codeigniter 2.0

I'm very new in codeigniter. I have a problem when upload multi file in codeigniter.
My upload form can upload multi files, resize and save them to database, everything is Ok, only one thing is not ok : not display error. Example: maximum file is 2MB, i choose file 3MB, it's not display message "The file you are attempting to upload is larger than the permitted size." The same error when i click button upload but not choose a file. The view will display like a image as link. (i'm not enought reputation to post images)
https://drive.google.com/file/d/0B2zpQIbKaLa6Z2Z6LXFrNXlVRUk/view?usp=sharing
This is my Controller:upload.php
private function _upload_files($field='userfile'){
$files = array();
foreach( $_FILES[$field] as $key => $all )
foreach( $all as $i => $val )
$files[$i][$key] = $val;
$files_uploaded = array();
for ($i=0; $i < count($files); $i++) {
$_FILES[$field] = $files[$i];
if ($this->upload->do_upload($field))
$files_uploaded[$i] = $this->upload->data($files);
else
//$files_uploaded[$i] = null;
$files_uploaded[$i] = $this->upload->display_errors();
}
return $files_uploaded;
}
public function do_upload(){
$data['error']="";
$this->_data['loadPage']="upload/upload_view";
$this->load->model('Mcategorie');
$this->_data["categories"] = $this->Mcategorie->listAllCate();
$this->load->model('Malbum');
$this->_data['albums']=$this->Malbum->listAllAlbum1();
$user_folder = './uploads/'.$this->session->userdata('username');
$thumb_folder = './uploads/'.$this->session->userdata('username').'/thumbnail/';
if(!is_dir($user_folder)){
mkdir($user_folder, 0777);
}
if(is_dir($user_folder)){
if(!is_dir($thumb_folder))
mkdir($thumb_folder,0777);
}
if($this->input->post("ok")){
$config['upload_path'] = $user_folder;
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '2048';
$config['max_width'] = '';
$config['max_height'] = '';
$config['overwrite'] = FALSE;
$config['remove_spaces'] = TRUE;
$this->load->library("upload",$config);
$this->load->library("image_lib");
if ($_FILES['image_list']) {
$images_upload= $this->_upload_files('image_list');
//echo "<pre>";
//print_r($images_upload);exit;
foreach($images_upload as $data){
$config['image_library'] = 'gd2';
$config['source_image'] = './uploads/'.$this->session->userdata('username').'/'.$data['file_name'];
$config['create_thumb'] = TRUE;
$config['new_image'] = './uploads/'.$this->session->userdata('username').'/thumbnail/'.$data['file_name'];
$config['maintain_ratio'] = FALSE;
$config['width'] = 400;
$config['height'] = 300;
$this->image_lib->initialize($config);
$this->image_lib->resize();
$file = array(
'name' => $data['raw_name'].$data['file_ext'],
'thumb_name' => 'thumb_'.$data['raw_name'].$data['file_ext'],
'date' => date("Y-m-d"),
'userid' => $this->session->userdata('userid'),
'username' => $this->session->userdata('username'),
'description' => $this->input->post("description"),
'roleid' => $this->session->userdata("roleid"),
'public' => $this->input->post("kiet"),
'albumid' => $this->input->post("album"),
'categoryid' => $this->input->post("cat"),
);
$this->Mupload->insert_images($file);
}
redirect(base_url()."default/user/profile","refresh");
}
}
$this->load->view ( $this->_data['path'], $this->_data);
}
This is my view: upload_view.php
<div class="upload container col-xs-12 col-sm-12 col-md-12 col-lg-12">
<div class="panel panel-info">
<div class="panel-heading"><strong>Upload Files</strong> <small>Sao Bac Dau Photo Sharing</small></div>
<div class="panel-body">
<!-- Standar Form -->
<form action="<?php echo base_url()."default/upload/do_upload"?>" method="post" enctype="multipart/form-data">
<div class="col-xs-6 col-sm-6 col-md-6 col-lg-6">
<label> Chọn file ảnh cần upload (jpg, jpeg, gif, png) : </label>
<div class="error col-xs-12 col-sm-12 col-md-12 col-lg-12 clear-css" style="color:#FF0081; font-weight: bold">
<?php if(isset($error)){echo $error; }?>
</div>
<div class="input-group">
<span class="input-group-btn" >
<span class="btn btn-primary btn-file">
Browse … <input type="file" multiple id="image_list" name="image_list[]" accept="image/*" >
</span>
</span>
<input type="text" class="form-control" readonly>
</div>
<input type="submit" name="ok" class="btn btn-md btn-primary" value="Upload Photo" >
</div>
</form>
</div>
</div>
What should i do to display error ? Sorry if my english is so bad. thank in advance for your help.
You will store error in $files_uploaded[$i] = $this->upload->display_errors(); but not use of this to display error so you need to fetch error from this array and display it on view.
Please see below code.
<?php
class Upload extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
function index()
{
$this->load->view('upload_form', array('error' => ' ' ));
}
function do_upload()
{
$config['upload_path'] = './uploads/';
$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());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
}
?>

how to create form for multple image upload in codeigniter

i have created form for multiple image upload.but not working that form.only one image uploading..i want multiple images are to be upload to folder and save image name in database..
My View File
<html>
<head>
<title>Product Upload</title>
<style>
#container
{
width:750px;
margin:0 auto;
//border:5px solid #000000;
}
#container form input[type="text"] {
height:30px;
}
</style>
</head>
<body>
<div id="container" align="center">
<form name="product" action="<?php echo base_url;?>admin/login/upload" method="POST" enctype="multipart/form-data" class="form-horizontal">
<table>
<h3>Add New Product</h3>
<tr><td>Categories</td><td><select name="catid"><option>Categories</option>
<?php if(isset($category_details))
{foreach($category_details as $keys=>$values){ ?>
<option value="<?php echo $values['cat_id'];?>"><?php echo $values['cat_name'];?></option>
<?php }
}?>
</select></td></tr>
<tr>
<td>Product Name:</td><td><input type="text" name="pname"></td></tr>
<tr><td><input type="file" multiple="true" name="userfile" size="20" /></td></tr>
<tr><td><br>Product Image:</td><td><br><input type="file" name="pimage[]" id="pimage" multiple></td></tr>
<tr><td><br>Description:</td><td><br><textarea name="pdescription"></textarea></td></tr>
<tr><td><br>Price:</td><td><br><input type="text" name="price"></td></tr>
<tr><td colspan="2" align="center"><br><input type="submit" name="submit" value="ADD" class="btn btn-primary"></td></tr>
</table>
</form>
</div>
</body>
</html>
My Controller File
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* #see http://codeigniter.com/user_guide/general/urls.html
*/
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->load->model('admin/category_model');
$this->load->model('admin/loginauth_model');
$this->load->model('login_model');
$this->load->library('session');
$this->load->helper('url');
$this->load->helper('cookie');
$this->load->library('encrypt');
$this->load->library('image_CRUD');
}
public function index()
{
$this->load->view('admin/login');
}
public function loadproduct()
{
$category_details=$this->category_model->getCategoryDetails();
$outputdata["category_details"]=$category_details;
$this->load->view('admin/product',$outputdata);
}
public function loginAuth()
{
$this->form_validation->set_rules('email','Enter Email','required|trim|max_length[50]|xss_clean');
$this->form_validation->set_rules('password','Enter your Password','required|trim|max_length[50]|xss_clean');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('admin/login');
}
else
{
$username=$_POST['email'];
$password=$_POST['password'];
$user_details=$this->loginauth_model->logincheck($username,$password);
//print_r($checkauth);
if($user_details)
{
if($this->session->userdata('adminusername'))
{
$adminusername=$this->session->userdata('adminusername');
$outputdata['username']=$adminusername;
}
$category_details=$this->category_model->getCategoryDetails();
$outputdata['category_details']=$category_details;
$this->load->view('admin/dashboard',$outputdata);
}
}
}
public function category()
{
//$this->load->view('admin/category');
$this->form_validation->set_rules('cat_name','category name','required|trim|max_length[50]|xss_clean');
$this->form_validation->set_rules('cat_desc','category description','required|trim|max_length[50]|xss_clean');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('admin/category');
}
else
{
$addcategory=$this->category_model->addcategory($_POST);
if($addcategory)
{
//**************************pending
$category_details=$this->category_model->getCategoryDetails();
$outputdata['category_details']=$category_details;
//print_r($outputdata);
$this->load->view('admin/categorylist',$outputdata);
}
}
}
public function categorylist()
{
//echo image_url;
$category_details=$this->category_model->getCategoryDetails();
$outputdata['category_details']=$category_details;
$outputdata['image_url']=image_url;
$this->load->view('admin/categorylist',$outputdata);
$this->load->view('admin/category');
}
public function userdetails()
{
$user_details=$this->login_model->userdetails();
$outputdata['user_details']=$user_details;
$this->load->view('admin/userdetails',$outputdata);
}
public function upload()
{
$productname=$_POST["pname"];
$description=$_POST["pdescription"];
$price=$_POST["price"];
$catid=$_POST["catid"];
$name_array = array();
echo $count = count($_FILES['pimage']['size']);
foreach($_FILES as $key=>$value)
for($s=0; $s<=$count-1; $s++) {
$_FILES['pimage']['name']=$value['name'][$s];
$_FILES['pimage']['type'] = $value['type'][$s];
$_FILES['pimage']['tmp_name'] = $value['tmp_name'][$s];
$_FILES['pimage']['error'] = $value['error'][$s];
$_FILES['pimage']['size'] = $value['size'][$s];
$config['upload_path'] = FCPATH.'img/product_uploads/original/';
$config['allowed_types'] = 'gif|jpg|png|jpeg|GIF|PNG|JPG|JPEG';
$config['max_size'] = '100';
$config['max_width'] = '150';
$config['max_height'] = '180';
$this->load->library('upload', $config);
$this->upload->do_upload();
$data = $this->upload->data();
$name_array[] = $data['file_name'];
//$products=$this->category_model->addproduct($catid,$productname,$description,$imagename,$imagesize,$price,$path);
}
$names= implode(',', $name_array);
/* $this->load->database();
$db_data = array('id'=> NULL,
'name'=> $names);
$this->db->insert('testtable',$db_data);
*/ print_r($names);
//echo FCPATH;
//$productname=$_POST["pname"];
// $description=$_POST["pdescription"];
// $price=$_POST["price"];
//$catid=$_POST["catid"];
// $path = FCPATH.'img/product_uploads/original/';
// $valid_formats = array("jpg", "png", "gif", "bmp","jpeg","PNG","JPG","JPEG","GIF","BMP","PNG");
// if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST") {
// $imagename = $_FILES['pimage']['name'];
// $imagesize = $_FILES['pimage']['size'];
// list($txt, $ext) = explode(".", $imagename);
//$products=$this->category_model->addproduct($catid,$productname,$description,$imagename,$imagesize,$price,$path);
//print_r($products);
// $tmp = $_FILES['pimage']['tmp_name'];
//if(move_uploaded_file($tmp, $path.$imagename)) {
// $product_details=$this->category_model->getProductDetails();
//print_r($product_details);
//if(isset($products)){
//echo "aa";
// $this->loadproduct();
//}
// }
//else
// {
//echo "Image Upload Failed.";
//}
}
public function logout()
{
$newdata = array(
'adminuser_id' =>'',
'adminusername' =>'',
'adminemail' => '',
'logged_in' => FALSE,
);
$this->session->unset_userdata($newdata);
$this->session->sess_destroy();
$this->index();
}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
You can't just add a multiple attrinbute to <input type="file" />, that's not going to work.
You'll have to use a third-party module/plugin, like Uploadify, to get this functionality running.
You could also check this tutorial on how to integrate Uploadify in CodeIgniter (although I can't see anything CodeIgniterish in your code, but you still tagged the question as CI relevant)
a) Instead of writing this <input type="file" multiple="true" name="userfile" size="20" /> write <input type="file" name="userfile[]" size="20" />
b) Inside your controller you can do something like this:
$files = $_FILES;
$count = count($_FILES['userfile']['name']);
for($i=0; $i<$count; $i++)
{
$_FILES['userfile']['name']= $files['userfile']['name'][$i];
$_FILES['userfile']['type']= $files['userfile']['type'][$i];
$_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
$_FILES['userfile']['error']= $files['userfile']['error'][$i];
$_FILES['userfile']['size']= $files['userfile']['size'][$i];
$this->upload->initialize($config);
$this->upload->do_upload();
$image_data = $this->upload->data();
}
Use this to open your upload form :
echo form_open_multipart('admin/do_upload');
After put one or more :
<input type="file" name="name" size="20" />
And use a function like that :
public function do_upload() {
$config['upload_path'] = './assets/images/upload/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('admin/images', $error);
} else {
$data = array('upload_data' => $this->upload->data());
}
}

Uploading multiple file using codeigniter

I got a problem with multiple upload file using codeigniter. I've tried many method before but still no success.
Here is my code.
View
<?php echo form_open_multipart('staff/upload_files', array('id' => 'form_checkbox')); ?>
Inside table:
<tr>
<th width="20%">Select File :</th>
<td>
<input type="file" name="userfile[]" value="" /> <span class="green">[Max File Upload: 2MB]</span><br>
<input type="file" name="userfile[]" value="" /> <span class="green">[Max File Upload: 2MB]</span><br>
<input type="file" name="userfile[]" value="" /> <span class="green">[Max File Upload: 2MB]</span>
</td>
</tr>
Controller
function upload_files() {
$data['studentid'] = $studentid;
if ($this->input->post('submit')) {
$config['upload_path'] = './upload/announcement/collegecode/';
$config['allowed_types'] = 'jpg|png';
$config['max_size'] = '2048';
$config['max_width'] = '0';
$config['max_height'] = '0';
$config['overwrite'] = FALSE;
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
$this->session->set_flashdata('msg', 'error: ' . $this->upload->display_errors());
} else {
$this->session->set_flashdata('msg', 'success:Successfully Upload Student Photo');
}
}
$this->template->load('template', 'staff/announcement', $data);
}
I've also tried this method, but no file was uploaded.
$config['upload_path'] = './upload/announcement/collegecode/';
$path=$config['upload_path'];
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '2048';
$config['max_width'] = '1920';
$config['max_height'] = '1280';
$this->load->library('upload');
foreach ($_FILES as $key => $value)
{
if (!empty($key['userfile']))
{
$this->upload->initialize($config);
if (!$this->upload->do_upload($key))
{
$errors = $this->upload->display_errors();
flashMsg($errors);
}
else
{
// Code After Files Upload Success GOES HERE
}
}
}

Resources