1

I am not able to upload multiple images in a folder in codeigniter. I have used this reference Multiple image upload. Someone please help me. Atleast the author of this reference please help me to sort out this problem. Here is my code

View

<input type="file" name="images[]" class="file_input" multiple />
<input type="file" name="images[]" class="file_input" multiple />
<input type="file" name="images[]" class="file_input" multiple />
<input type="file" name="images[]" class="file_input" multiple />

Script

$(document).ready(function(){
$('#save').on('click', function(){
    var fileInputs = $('.file_input');
    var formData = new FormData();
    $.each(fileInputs, function(i,fileInput){
        if( fileInput.files.length > 0 ){
            $.each(fileInput.files, function(k,file){
                formData.append('images[]', file);
            });
        }
    });
    $.ajax({
        url: '<?php echo base_url(); ?>exerciseset/process', 
                    dataType: 'json', 
                    contentType: false,
                    processData: false,
                    data: formData,
                    method: 'post',

        success: function(response){
            console.log(response);
        }
    });
});
});

Controller

public function process()
{
$fileuploadurl = base_url() . "study_set/";
    $config['upload_path'] = 'study_set/';
    $config['allowed_types'] = 'gif|jpg|png|jpeg|bmp|mp3|mp4';
    $config['max_filename'] = '255';
    $config['encrypt_name'] = FALSE;
    $config['max_size'] = '25000000';
$F = array();

$count_uploaded_files = count( $_FILES['images']['name'] );

$files = $_FILES;
for( $i = 0; $i < $count_uploaded_files; $i++ )
{
    $_FILES['userfile'] = [$files['images']['name'][$i]];

    $F[] = $_FILES['userfile'];

    // Here is where you do your CodeIgniter upload ...
$this->load->library('upload', $config);
                $this->upload->data($_FILES['userfile']); 
                if (!$this->upload->data('images')) {
                    echo $this->upload->display_errors();
                } 
}

echo json_encode($F);
}

It returns the images name into array but not able to upload it into folder. And also i need each image name in separate variable. Please help me i am in hurry.

ahmed sharief
  • 83
  • 2
  • 13

1 Answers1

0

In your code it seems like you missed to upload file. Hence it will not moved to specific folder. Please check below, Hope it will help you.

public function process()
{
    $fileuploadurl = base_url() . "study_set/";
    $config['upload_path'] = APPPATH . 'uploads/study_set/';
    $config['allowed_types'] = 'gif|jpg|png|jpeg|bmp|mp3|mp4';
    $config['max_filename'] = '255';
    $config['encrypt_name'] = FALSE;
    $config['max_size'] = '25000000';
    $F = array();

    $count_uploaded_files = count( $_FILES['images']);
    $result = array();
    for( $i = 0; $i < $count_uploaded_files; $i++ )
    {
        $_FILES["file"]["name"] = $_FILES["images"]["name"][$i];
        $_FILES["file"]["type"] = $_FILES["images"]["type"][$i];
        $_FILES["file"]["tmp_name"] = $_FILES["images"]["tmp_name"][$i];
        $_FILES["file"]["error"] = $_FILES["images"]["error"][$i];
        $_FILES["file"]["size"] = $_FILES["images"]["size"][$i];
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('file')) {
            $result['errors'][] = $this->upload->display_errors();
        }else{ 
            $result['success'][] = $this->upload->data(); 
        }
    }
    echo json_encode($result);
}

Please make sure you have specified correct folder path and folder has enough permissions to write files.

Let me know if it not works.

Alex Mac
  • 2,970
  • 1
  • 22
  • 39
  • I have given the permissions to write in that folder. Your code is not working. – ahmed sharief Oct 10 '17 at 07:23
  • Use debugger tool and share your ajax response, So I can help you better. – Alex Mac Oct 10 '17 at 07:26
  • (4) ["1.jpg", "2.jpg", "3.jpg", "4.jpg"]0: "1.jpg"1: "2.jpg"2: "3.jpg"3: "4.jpg"length: 4__proto__: Array(0) – ahmed sharief Oct 10 '17 at 07:38
  • I am getting the image from the fields to the controller only it is not updating in the folder – ahmed sharief Oct 10 '17 at 07:44
  • It seems like you are doing something wrong, If you gave used above code than you will get two array in response. Array of success and errors. Please check your code again or copy and paste my whole code and check. also add your upload path like `APPPATH . 'uploads/study_set/';` – Alex Mac Oct 10 '17 at 08:14
  • I had added the response please check it – ahmed sharief Oct 10 '17 at 08:39
  • I have updated my answer. Where is your study_set is located in sile structure? Can you please specify your path..? – Alex Mac Oct 10 '17 at 09:11