Multiple files upload in Codeigniter
123,487
Solution 1
I change upload method with images[]
according to @Denmark.
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;
}
Solution 2
You should use this library for multi upload in CI https://github.com/stvnthomas/CodeIgniter-Multi-Upload
Solution 3
public function index() {
$user = $this->session->userdata("username");
$file_path = "./images/" . $user . '/';
if (isset($_FILES['multipleUpload'])) {
if (!is_dir('images/' . $user)) {
mkdir('./images/' . $user, 0777, TRUE);
}
$files = $_FILES;
$cpt = count($_FILES ['multipleUpload'] ['name']);
for ($i = 0; $i < $cpt; $i ++) {
$name = time().$files ['multipleUpload'] ['name'] [$i];
$_FILES ['multipleUpload'] ['name'] = $name;
$_FILES ['multipleUpload'] ['type'] = $files ['multipleUpload'] ['type'] [$i];
$_FILES ['multipleUpload'] ['tmp_name'] = $files ['multipleUpload'] ['tmp_name'] [$i];
$_FILES ['multipleUpload'] ['error'] = $files ['multipleUpload'] ['error'] [$i];
$_FILES ['multipleUpload'] ['size'] = $files ['multipleUpload'] ['size'] [$i];
$this->upload->initialize($this->set_upload_options($file_path));
if(!($this->upload->do_upload('multipleUpload')) || $files ['multipleUpload'] ['error'] [$i] !=0)
{
print_r($this->upload->display_errors());
}
else
{
$this->load->model('uploadModel','um');
$this->um->insertRecord($user,$name);
}
}
} else {
$this->load->view('uploadForm');
}
}
public function set_upload_options($file_path) {
// upload an image options
$config = array();
$config ['upload_path'] = $file_path;
$config ['allowed_types'] = 'gif|jpg|png';
return $config;
}
Solution 4
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 true;
}
Author by
Dinuka Thilanga
I was software engineer at eMarketingeye. Now i am Senior Software engineer at eFutures.
Updated on October 19, 2020Comments
-
Dinuka Thilanga over 3 years
I want to upload multiple files using single element. So I try this example.
Multiple files upload (Array) with CodeIgniter 2.0
This is my form
<form enctype="multipart/form-data" class="jNice" accept-charset="utf-8" method="post" action="http://xxxx.dev/admin/add_estates"> <fieldset> <label>Title * : </label> <input type="text" class="text-long" value="" name="title"> <label>Description : </label> <textarea class="mceEditor" rows="10" cols="40" name="description"></textarea> <label>Image : </label> <input type="file" multiple="" name="images[]"> <button class="button-submit" type="submit" name="save" id="">Save</button> </fieldset> </form>
This is my controller
public function add_estates() { $data['page_title'] = "© IDEAL - Administrator - Real State - Add Real Estate"; $data['main_content'] = 'admin/add_estates'; if ($this->input->post() !== FALSE) { $this->load->library('form_validation'); $this->form_validation->set_rules('title', 'Career Title', 'trim|required'); if ($this->form_validation->run() !== FALSE) { $title = $this->input->post('title'); $description = $this->input->post('description'); if (!empty($_FILES['images']['name'][0])) { if ($this->upload_files($title, $_FILES['images']) === FALSE) { $data['error'] = $this->upload->display_errors('<div class="alert alert-danger">', '</div>'); } } if (!isset($data['error'])) { $this->admin_model->add_estate($title, $description, $image_name); $this->session->set_flashdata('suc_msg', 'New real estate added successfully'); redirect('admin/add_estates'); } } } $data['suc_msg'] = $this->session->flashdata('suc_msg'); $this->load->view('layout_admin', $data); }
This is my file upload method
private function upload_files($title, $files) { $config = array( 'upload_path' => './upload/real_estate/', 'allowed_types' => 'jpg|gif|png', 'overwrite' => 1, ); $this->load->library('upload', $config); 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]; $config['file_name'] = $title .'_'. $image; $this->upload->initialize($config); if ($this->upload->do_upload($image)) { $this->upload->data(); } else { return false; } } return true; }
But it give
You did not select a file to upload.
every time. What is the issue? -
Deepak Keynes almost 8 yearsok , what kind of file is .fr3 files? pdf is application, html is text, fr3 is?
-
saintfalcon over 6 years.fr3 = FastReport template file?
-
Dinuka Thilanga over 6 yearsWhere .fr3 is mentioned here?
-
Dinuka Thilanga over 6 yearsWhat different? Only return type is difference?
-
Rishi over 6 yearsThe following code uploads multiple files as well as creates a directory for each user and upload the files in that directory and the input element is as following ===> <input type="file" name="multipleUpload[]" multiple="multiple">
-
Md Ashraful Islam over 3 yearsError: Undefined index: name after the line foreach... What do do?
-
Octopus about 3 years$_FILES is one of PHP's built in $GLOBALS. You definitely should not be assigning anything to it ever. This is very bad practice.