Viewed   89 times

I am new user to using code igniter in my project, I am facing one problem while uploading multiple files but the last one only insert to all image three images field.

my controller is:

function products()
    {
     date_default_timezone_set("Asia/Kolkata");
     $config['upload_path'] = './resources/images/products/';
     $config['allowed_types']        = 'gif|jpg|png';
     $config['max_size']             = 1000;
     $config['max_width']            = 1024;
     $config['max_height']           = 768;
     $this->load->library('upload', $config);
     $this->upload->do_upload('userfile');
        $data = array('prod_image' => $this->upload->data(), 
             'prod_image1' => $this->upload->data(),
              'prod_image2' => $this->upload->data());
    $product_image=$data['prod_image']['file_name']; 
    $product_image1=$data['prod_image1']['file_name'];
    $product_image2=$data['prod_image2']['file_name'];

        $data = array(
                'name' => $this->input->post('pd_name'),
                'prod_image' => $product_image,
                'prod_image1' => $product_image1,
                'prod_image2' => $product_image2,
                'created_time' => date('Y-m-d H:i:s'));

        // insert form data into database
        $result_set= $this->tbl_products_model->insertUser($data);

    }       

my view part is:

<input class="form-control" name="pd_name"type="text"/>
<input type="file"  class="file_upload2" name="userfile"/> //1
<input type="file" class="file_upload2" name="userfile"/> //2
<input type="file" class="file_upload2" name="userfile"/>//3

Please help how to insert 3 images.

my datad base like

===========================================
id|name|prod_image|prod_image1|prod_image2|
===========================================
 1|ard|           |           |          |
============================================

 Answers

2

Html :

<input type="file" name="userfile[]" multiple="multiple">

PHP :

<?php
public function products()
{       
    $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();
    }

    $data = array(
        'name' => $this->input->post('pd_name'),
        'prod_image' => $dataInfo[0]['file_name'],
        'prod_image1' => $dataInfo[1]['file_name'],
        'prod_image2' => $dataInfo[2]['file_name'],
        'created_time' => date('Y-m-d H:i:s')
     );
     $result_set = $this->tbl_products_model->insertUser($data);
}

private function set_upload_options()
{   
    //upload an image options
    $config = array();
    $config['upload_path'] = './resources/images/products/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size']      = '0';
    $config['overwrite']     = FALSE;

    return $config;
}
?>
Thursday, October 27, 2022
4

At your PHP change

$this->upload->do_upload()

to

$this->upload->do_upload("add_1")

where "add_1" is your input name attribute.

Codeigniter UPLOAD library docs

P.S. At your HTML the Label attribute for="" is targeting input ID so you have to add id to your input.

Thursday, September 15, 2022
5

if you want to upload a file in diferents folders by user or whatever you need to do this

public function upload_f(){
    $config['upload_path']      = FCPATH . '/files/automation/' . $id . '/';
    $config['allowed_types']    = 'xls|xlsx';

    $this->load->library('upload', $config);

    if( ! $this->upload->do_upload()){
        $this->session->set_flashdata('upload-no', $this->upload->display_errors());
    }
    else{
        $this->_read_file(FCPATH . '/files/automation/' . $id . '/' . $this->upload->file_name);
    }
}

private funtion _read_file( $file ){
    $this->load->library('excel');
    $this->load->library('table');

    $file               = str_replace('//', '/', $file);

    $objPHPExcel = PHPExcel_IOFactory::load($file);

    $cell_collection    = $objPHPExcel->getActiveSheet()->getCellCollection();
    $lastRow            = $objPHPExcel->getActiveSheet()->getHighestRow();

    foreach ($cell_collection as $cell) {
        $column = $objPHPExcel->getActiveSheet()->getCell($cell)->getColumn();
        $row    = $objPHPExcel->getActiveSheet()->getCell($cell)->getRow();
        $data_value = $objPHPExcel->getActiveSheet()->getCell($cell)->getValue();

        if ($row == 1) {
            $header[$row][$column] = $data_value;
        } 
        else{
            $arr_data[$row][$column] = $data_value;
        }
    }

    $this->table->set_heading('ID', 'value1', 'value2');

    for($i = 2; $i <= $lastRow; $i++){
        $_table= array($i, $arr_data[$i]['A'], $arr_data[$i]['B']);

        $this->table->add_row($_table);
    }

    echo $this->table->generate();
}
Tuesday, September 6, 2022
 
diryboy
 
1
extract($_POST);
$error=array();
$extension=array("jpeg","jpg","png","gif");
foreach($_FILES["files"]["tmp_name"] as $key=>$tmp_name) {
    $file_name=$_FILES["files"]["name"][$key];
    $file_tmp=$_FILES["files"]["tmp_name"][$key];
    $ext=pathinfo($file_name,PATHINFO_EXTENSION);

    if(in_array($ext,$extension)) {
        if(!file_exists("photo_gallery/".$txtGalleryName."/".$file_name)) {
            move_uploaded_file($file_tmp=$_FILES["files"]["tmp_name"][$key],"photo_gallery/".$txtGalleryName."/".$file_name);
        }
        else {
            $filename=basename($file_name,$ext);
            $newFileName=$filename.time().".".$ext;
            move_uploaded_file($file_tmp=$_FILES["files"]["tmp_name"][$key],"photo_gallery/".$txtGalleryName."/".$newFileName);
        }
    }
    else {
        array_push($error,"$file_name, ");
    }
}

and you must check your HTML code

<form action="create_photo_gallery.php" method="post" enctype="multipart/form-data">
    <table width="100%">
        <tr>
            <td>Select Photo (one or multiple):</td>
            <td><input type="file" name="files[]" multiple/></td>
        </tr>
        <tr>
            <td colspan="2" align="center">Note: Supported image format: .jpeg, .jpg, .png, .gif</td>
        </tr>
        <tr>
            <td colspan="2" align="center"><input type="submit" value="Create Gallery" id="selectedButton"/></td>
        </tr>
    </table>
</form>
Sunday, September 25, 2022
3

Okay a few minor changes might help

public function post(){        
    if($this->_validation()===FALSE){ 
        $this->session->set_flashdata('error', 'Ooops, there was an error');
        redirect(base_url("admin/product"));
    }else{
        $files = $_FILES;
        $images = 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();
            $images[] = $_FILES['userfile']['name'];
        }
        $fileName = implode(',',$images);

        $data = array(  'kodeProduk'                => $this->input->post('kodeproduk'),
                        'ket'                       => $this->input->post('ket'),

                        'GambarBesar'               => $fileName
        );

        unset($data['submit']);                             
        $this->table->add_record($data);
        $this->session->set_flashdata('success', 'Product has been saved.');
        redirect(base_url("admin/product"));
    }   
}
Tuesday, October 25, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :