Multiple file upload with Codeigniter 3 - codeigniter

I am trying to upload multiple images.
My form is this:
<form>
<input type="file" name="images[]">
<input type="file" name="images[]">
<input type="file" name="images[]">
</form>
I know that this form is right. I am getting an error when Codeigniter tries to upload a file.
function do_upload_images() {
$files = $_FILES;
$cpt = count ( $_FILES ['images'] ['name'] );
for($i = 0; $i < $cpt; $i ++) {
$_FILES ['images'] ['name'] = $files ['images'] ['name'] [$i];
$_FILES ['images'] ['type'] = $files ['images'] ['type'] [$i];
$_FILES ['images'] ['tmp_name'] = $files ['images'] ['tmp_name'] [$i];
$_FILES ['images'] ['error'] = $files ['images'] ['error'] [$i];
$_FILES ['images'] ['size'] = $files ['images'] ['size'] [$i];
$this->upload->initialize ( $this->set_upload_options () );
$this->upload->do_upload ($_FILES['images']);
}
}
private function set_upload_options() {
// upload an image options
$config = array ();
$config ['upload_path'] = './uploads/estate_images';
$config ['allowed_types'] = 'gif|jpg|png';
$config ['encrypt_name'] = TRUE;
return $config;
}
This is the error i got:
Message: Illegal offset type in isset or empty Filename:
libraries/Upload.php Line Number: 377

I get the solution by myself. Just needed to change the following line:
$this->upload->do_upload ($_FILES['images']);
to
$this->upload->do_upload ('images');

Related

Codeigniter Multiple upload files

Good day Everyone who had an idea on multiple file upload. i want to achieved on how to send multiple file to be inserted in my datatables.
this my view page
<div class="form-group">
<input type="file" name="file">
<button type="submit" class="btn btn-success">Add New Record</button>
</div>`enter code here`
this is my function that will upload in the database
$path = './files';
$ckdir = is_dir($path);
if(!$ckdir){
mkdir($path, 0777, true);
}
$userid = $this->session->userdata("sessionid");
$filename =$path."/".$userid."-".$_FILES["file"]["name"];
if(!file_exists($filename."pdf")){
move_uploaded_file($_FILES["file"]["tmp_name"], $filename);
}
else{
return false;
}
View File:
<div class="form-group">
<input type="file" name="file[]">
<button type="submit" class="btn btn-success">Add New Record</button>
</div>
Controller File:
$path = './files';
$ckdir = is_dir ( $path );
if (! $ckdir) {
mkdir ( $path, 0777, true );
}
$countfiles = count ( $_FILES ['files'] ['name'] );
// Looping all files
for($i = 0; $i < $countfiles; $i ++) {
if (! empty ( $_FILES ['files'] ['name'] [$i] )) {
// Define new $_FILES array - $_FILES['file']
$_FILES ['file'] ['name'] = $_FILES ['files'] ['name'] [$i];
$_FILES ['file'] ['type'] = $_FILES ['files'] ['type'] [$i];
$_FILES ['file'] ['tmp_name'] = $_FILES ['files'] ['tmp_name'] [$i];
$_FILES ['file'] ['error'] = $_FILES ['files'] ['error'] [$i];
$_FILES ['file'] ['size'] = $_FILES ['files'] ['size'] [$i];
// Set preference
$config ['upload_path'] = $path;
$config ['allowed_types'] = '*';
$config ['file_name'] = $_FILES ['files'] ['name'] [$i];
// Load upload library
$this->load->library ( 'upload', $config );
// File upload
if ($this->upload->do_upload ( 'file' )) {
// Get data about the file
$uploadData = $this->upload->data ();
$filename = $uploadData ['file_name'];
// Initialize array
$data ['filenames'] [] = $filename;
}
}
}

Update Selected file on Multiple Upload CodeIgniter

I've write the codeigniter for three upload file. And when i update one or two, the third will be overwrite data that i've upload before with blank in db. how to make it update only the file that i need to . i've try if !empty $_FILES but my head start smoky :(
<input type="file" class="form-control" name="userfile[]">Front
<input type="file" class="form-control" name="userfile[]">Back
<input type="file" class="form-control" name="userfile[]">Side
this my controller
public function prosesUpdate2(){
$data = $this->input->post('id');
$this->load->library('upload');
$dataInfo = array();
$files = $_FILES;
$cpt = count($_FILES['userfile']['name']);
for($i=0; $i<$cpt; $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($this->set_upload_options());
$this->upload->do_upload();
$dataInfo[] = $this->upload->data();
}
$files = array(
'front' => $dataInfo[0]['file_name'],
'back' => $dataInfo[1]['file_name'],
'side' => $dataInfo[2]['file_name']
// 'userdatecreate' => date('Y-m-d H:i:s')
);
$result_set = $this->update_building->db_update($files, $data);
$this->session->set_flashdata('file_success', 'Upload File Success!');
}
the model
public function db_update($data,$id)
{
$this->db->where('id', $id);
$this->db->update('allbuidingdata', $data);
}
Please check the posted files before the update.
Slightly modify your code.
public function prosesUpdate2(){
$data = $this->input->post('id');
$this->load->library('upload');
$dataInfo = array();
$files = $_FILES;
$files_to_update = array();
$cpt = count($_FILES['userfile']['name']);
for($i=0; $i<$cpt; $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($this->set_upload_options());
$this->upload->do_upload();
$dataInfo[] = $this->upload->data();
}
$files_to_update= array(
'userdatecreate' => date('Y-m-d H:i:s')
);
if($dataInfo[0]['file_name']){
$files_to_update['front'] = $dataInfo[0]['file_name'];
}
if($dataInfo[1]['file_name']){
$files_to_update['back'] = $dataInfo[1]['file_name'];
}
if($dataInfo[2]['file_name']){
$files_to_update['side'] = $dataInfo[2]['file_name'];
}
$result_set = $this->update_building->db_update($files_to_update, $data);
$this->session->set_flashdata('file_success', 'Upload File Success!');
}
You forgot to place the field name in do_upload function
$this->upload->do_upload('userfile');
Hope this helps !

How to upload multiple photos in codeiginter?

I am trying to upload multiple photos! My code is working but it's uploading only one photo - not all selected photos.
What's wrong in my code?
if(count($_FILES["userfile"]["name"]) == 0) {
$this->session->set_flashdata('success', '?? ????? ?????? ?????');
redirect('accidents/index');
}
else {
// configurations from upload library
$config['upload_path'] = './uploads/images';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '2048000'; // max size in KB
$config['max_width'] = '20000'; //max resolution width
$config['max_height'] = '20000'; //max resolution height
// load CI libarary called upload
$this->load->library('upload', $config);
for($count = 0; $count < count($_FILES["userfile"]["name"]); $count++) {
// body of if clause will be executed when image uploading is failed
if(!$this->upload->do_upload()) {
$errors = array('error' => $this->upload->display_errors());
// This image is uploaded by deafult if the selected image in not uploaded
$image = 'no_image.png';
}
// body of else clause will be executed when image uploading is succeeded
else {
$data = array('upload_data' => $this->upload->data());
$image = $_FILES['userfile']['name']; //name must be userfile
}
$this->accidents_model->addphoto($image,$last_id);
}
}
And the model is:
public function addphoto($photo,$last_id) {
$data = array(
'cp_photo' => $photo,
'ac_id' => $last_id
);
//insert image to the database
$this->db->insert('cars_photos', $data);
}
i found the problem in my code in for loop it was upload one file after that redirected to index page ( this is mistake ) case the redirect line must be outside for loop :)
Here full working code after some modification with able to create folder for each user id (i take the code from stack usere here :) thanks alot )
in upload page i use
public function upload() {
$acc = $last_id;
$file_path = ".uploads/images/" . $acc . '/';
if (isset($_FILES['multipleUpload'])) {
if (!is_dir('uploads/images/' . $acc)) {
mkdir('.uploads/images/' . $acc, 0777, TRUE);
}
$files = $_FILES;
$cpt = count($_FILES ['multipleUpload'] ['name']);
$this->load->library('upload');
for ($i = 0; $i < $cpt; $i ++) {
$name = $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->accidents_model->addphoto($name,$acc);
}
}
//======================================================================================
}
$this->session->set_flashdata('success', 'the files uploaded');
redirect('accidents/index'); // :) here must located outside for loop
}
}
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;
}

Display all subfolders of folder in CodeIgniter

I am trying to display my uploaded content in my view page.
I have uploaded some files to my assets folder. In this the applicant (here) has an id, and uploading documents also have an id. During uploading these document ids are inserted in db with commas.
The uploaded path is ./assets/uploads/applicant_id/document_id.
I want to display the all files from this path. Problem is that when l have uploaded a document, and display only its (last uploaded) document files .
function index()
{
$application_id=$this->session->userdata('application_id');
$this->load->helper('directory');
$document_details = $this->home_model->get_document_details();
$$document_ids = $this->home_model->get_evidence_ids($application_id);
$$document_id= $evidence_ids->applicant_evidence_id;
$document= explode(',',$evidence_id);
for($i=0; $i < count($evidence); $i++)
{
$uploaded_files = array();
$uploaded_files = directory_map('./application/assets/uploads/'.$application_id.'/'.$document_id[$i]);
}
$data=array(
'document'=>$document,
'document_details'=>$document_details,
'page_name'=>'Home',
'dashboard_index'=>1,
'uploaded_files' =>$uploaded_files,
'application_id'=>$application_id,
'head_extra'=>'<link rel="stylesheet" type="text/css" href="'.base_url().'application/assets/css/home.css"> ',
'footer_extra'=>'',
);
$this->load->view('header',$data);
$this->load->view('index',$data);
$this->load->view('footer',$data);
}
function do_upload()
{
$evidence_id = $_POST['document_id'];
$application_id=$this->session->userdata('application_id');
$this->load->library('upload');
$files = $_FILES;
$cpt = count($_FILES['userfile']['name']);
for($i=0; $i<$cpt; $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($this->set_upload_options($document_id,$application_id));
$this->upload->do_upload();
}
$evidence = $this->home_model->insert_evidence($application_id,$document_id);
$success_message = "Document Uploaded.";
$this->session->set_flashdata('success_message',$success_message);
redirect(base_url().'home','refresh');
}
private function set_upload_options($document_id,$application_id)
{
if(!is_dir('./application/assets/uploads/'.$application_id.'/'.$document_id))
{
mkdir('./application/assets/uploads/'.$application_id.'/'.$document_id, 0777, TRUE);
}
$config = array();
$config['upload_path'] = './application/assets/uploads/'.$application_id.'/'.$document_id;
$config['allowed_types'] = 'jpg|png|bmp|jpeg|gif|vnd.ms-excel|vnd.openxmlformats-officedocument.spreadsheetml.sheet|csv|pdf';
$config['max_size'] = '0';
$config['overwrite'] = FALSE;
return $config;
}
view
<div class="portal-body">
<?php $file_count = count($uploaded_files);
for($i=0;$i<$file_count;$i++)
{ ?>
<p><?php echo $uploaded_files[$i]; ?></p>
<?php } ?>
</div>
First get the images from database
For ex.
$images = image1,image2,image3;
$newImages = explode(',',$images);
Now use foreach loop
foreach($newImages as $img)
{
//check for file type
$name = $img;
$info = new SplFileInfo($name);
$extension = $info->getExtension();
//echo $extension;
if($extension == "jpg" || $extension == "png" || $extension == "gif" || $extension == "jpeg") {
<img src="<?php echo base_url('your file location/.$img');?>">;
}else{
show your file here
}
}
For more read this tutorial
http://w3code.in/2015/09/upload-file-using-codeigniter/

i have a working multiple upload (document and image) CODEIGNITER

I have made a working multiple upload in codeigniter but I'm having a problem on how am I gonna insert the file names of those files on two tables (documents and images table) which these two tables have two the same column name (ID, name). is there way that i could disjunct or compart my code for uploading image and doc. because I united them in one function.
here is my CODE. it is working.
VIEW
<?php echo form_open_multipart('test'); ?>
<label>Images</label>
<input type='file' multiple='multiple' name='userfile[]'>
<label>Documents</label>
<input type='file' multiple='multiple' name='userfile[]'>
<?php echo form_submit('submit', 'Upload them files!') ?>
CONTROLLER
function index()
{
if (isset($_POST['submit']))
{
$this->load->library('upload');
//$this->uploadfile($_FILES['userfile']);
$files = $_FILES;
$cpt = count($_FILES['userfile']['name']);
for($i=0; $i<$cpt; $i++)
{
$filename = $_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($this->set_upload_options());
$this->upload->do_upload();
$this->upload->data();
}
}
$this->load->view("test");
}
private function set_upload_options()
{
// upload an image and document options
$config = array();
$config['upload_path'] = './upload_documents/';
$config['allowed_types'] = 'jpg|png|gif|jpeg|JPG|PNG|GIF|JPEG|pdf|doc|docx|xls|xlsx';
$config['max_size'] = '0'; // 0 = no file size limit
$config['max_width'] = '0';
$config['max_height'] = '0';
$config['overwrite'] = TRUE;
return $config;
}
These codes are working and it is able to transfer all the files on my desired path.
but I was wondering on making the MODEL, how am I gonna identify the file type as you seen my codes above I passed the name of the file on a variable "$filename". If you use print_r($filename), you'll see all the file name and it's file extension. Those names is the one that I will insert to my two tables accordingly to their type of file.
Is there any code for CodeIgniter or PHP code that I will use to identify the file type and pass it to the model with two function like upload_image or upload_docu? Help please.
so here it is..
CONTROLLER
function index()
{
if (isset($_POST['submit']))
{
$this->load->library('upload');
//$this->uploadfile($_FILES['userfile']);
$files = $_FILES;
$cpt = count($_FILES['userfile']['name']);
for($i=0; $i<$cpt; $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($this->set_upload_options());
$this->upload->do_upload();
$this->upload->data();
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$img_ext_chk = array('jpg','png','gif','jpeg','JPG','PNG', 'GIF', 'JPEG');
if (in_array($ext,$img_ext_chk))
{
$this->asset->add_image($filename);
}
else
{
$this->asset->add_document($filename);
}
}
}
}
and to your MODEL
public function add_image($filename)
{
$data = array ('images' => $filename);
$this->db->insert('asset_images', $data);
}
public function add_document($filename)
{
$data = array ('documents' => $filename);
$this->db->insert('asset_documents', $data);
}

Resources