Multiple image uploading error in Codeigniter

ankit suthar

I am trying to upload the multiple images through my form. I have taken care of many expect like form_open_multipart, calling a name as Array in upload input.

I have no issue with the images upload. I can upload multiple images successfully, but the problem comes when I select any text or non-image file with the images.

I am checking scenarios that if user mixes it up (accidently select any other file with images) images with text file then some files are getting uploaded and then I get the error of the wrong type of file. In Ideal case, it may not allow uploading the file if any one file does not have the correct file type.

so can any one help me over that how can I check before file upload that all files are allowed type or not?

I tried many examples but get the same problem In all.

There is no problem if i upload the multiple images. It's work fine.

<input type="file" name="pic_url[]"  multiple accept="image/*" />

And this below is my controller

$config['upload_path'] = './assets/prop_pic/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = 20000;
    $config['encrypt_name'] = TRUE;
    $config['detect_mime'] = TRUE;
    //initialize upload class
    $this->load->library('upload', $config);

    $upload_error = array();

    for ($i = 0; $i < count($_FILES['pic_url']['name']); $i++) {

        $_FILES['pic_url']['name'] = $_FILES['pic_url']['name'][$i];
        $_FILES['pic_url']['type'] = $_FILES['pic_url']['type'][$i];
        $_FILES['pic_url']['tmp_name'] = $_FILES['pic_url']['tmp_name'][$i];
        $_FILES['pic_url']['error'] = $_FILES['pic_url']['error'][$i];
        $_FILES['pic_url']['size'] = $_FILES['pic_url']['size'][$i];

        if (!$this->upload->do_upload('pic_url')) {
            // fail
            $upload_error = array('error' => $this->upload->display_errors());
            //$this->load->view('multiple_upload_view', $upload_error);

            $this->session->set_flashdata('upload_error', $upload_error);
            redirect('property_master/view_prop_master_form');

            break;
        }
    }
Gavin

First problem is primarily down to how you handle your errors.

if (!$this->upload->do_upload('pic_url')) {
    // fail
    $upload_error = array('error' => $this->upload->display_errors());
    //$this->load->view('multiple_upload_view', $upload_error);

    $this->session->set_flashdata('upload_error', $upload_error);
    redirect('property_master/view_prop_master_form');

    break;
}

The above code will simply halt the rest of the for loop and redirect on the first error.

A simple solution would be to change that to log failures and continue processing:

$config['upload_path'] = './assets/prop_pic/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 20000;
$config['encrypt_name'] = TRUE;
$config['detect_mime'] = TRUE;
//initialize upload class
$this->load->library('upload', $config);

$upload_error = array();

for ($i = 0; $i < count($_FILES['pic_url']['name']); $i++) {

    $_FILES['pic_url']['name'] = $_FILES['pic_url']['name'][$i];
    $_FILES['pic_url']['type'] = $_FILES['pic_url']['type'][$i];
    $_FILES['pic_url']['tmp_name'] = $_FILES['pic_url']['tmp_name'][$i];
    $_FILES['pic_url']['error'] = $_FILES['pic_url']['error'][$i];
    $_FILES['pic_url']['size'] = $_FILES['pic_url']['size'][$i];

    if (!$this->upload->do_upload('pic_url')) {
        $upload_error[$i] = array('error' => $this->upload->display_errors());
    }
}

if (!empty($upload_error)) {
    $this->session->set_flashdata('upload_error', $upload_error);
    redirect('property_master/view_prop_master_form');
}

Alternatively, you would need to look at manually validating files based on their file extension.

$config['upload_path'] = './assets/prop_pic/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 20000;
$config['encrypt_name'] = TRUE;
$config['detect_mime'] = TRUE;
//initialize upload class
$this->load->library('upload', $config);

$upload_error = array();

foreach ($_FILES['pic_url'] as $key => $file) {

    $ext = pathinfo($file['name'], PATHINFO_EXTENSION);
    if (strpos($config['allowed_types'], $ext) === false) {
        $upload_error[$key] => array('error' => sprintf('<p>Invalid file extension (%s)</p>', $ext));
        continue;
    }

    $_FILES['userfile'] = $file;
    $this->upload->initialize($config);

    if (!$this->upload->do_upload('userfile')) {
        $upload_error[$key] = array('error' => $this->upload->display_errors());
    } else {
        // do something with $this->upload->data();
    }
}

if (!empty($upload_error)) {
    $this->session->set_flashdata('upload_error', $upload_error);
    redirect('property_master/view_prop_master_form');
}

Hope that helps

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

CodeIgniter: Image is not uploading.

From Dev

CodeIgniter: Image is not uploading.

From Dev

uploading an image in codeigniter trouble

From Dev

Uploading multiple images in codeigniter?

From Dev

Uploading multiple images in codeigniter?

From Dev

File uploading error in Codeigniter

From Dev

Multiple files uploading not working in Codeigniter

From Dev

Multiple files uploading not working in Codeigniter

From Dev

Error Uploading File with AJAX in codeigniter

From Dev

error uploading image to directory

From Dev

Error uploading image

From Dev

error uploading image to directory

From Dev

Error uploading image

From Dev

I am getting empty array, I need array of image while uploading multiple images in Codeigniter

From Dev

Uploading Image in db using Codeigniter 3.0.1

From Dev

Uploading Image in db using Codeigniter 3.0.1

From Dev

facing problems uploading image in a form using codeigniter

From Dev

uploading image in swift with multiple parameters

From Dev

Error uploading image using php

From Dev

Error in image uploading and renaming in php

From Dev

Error after uploading codeigniter website to server

From Dev

Multiple image upload error when uploading more than 20 images or so

From Dev

Error (Invalid argument supplied for foreach()) While uploading multiple image using php

From Dev

Media uploader "uploading" multiple instances of the same image

From Dev

Media uploader "uploading" multiple instances of the same image

From Dev

Uploading multiple image in yii2

From Dev

Codeigniter Uploading excel and Saving to database (Network Error (tcp_error))

From Dev

How to validate image aspect ratio before uploading in codeigniter

From Dev

Uploading an image of any format and showing it irrespective of the extension codeigniter

Related Related

  1. 1

    CodeIgniter: Image is not uploading.

  2. 2

    CodeIgniter: Image is not uploading.

  3. 3

    uploading an image in codeigniter trouble

  4. 4

    Uploading multiple images in codeigniter?

  5. 5

    Uploading multiple images in codeigniter?

  6. 6

    File uploading error in Codeigniter

  7. 7

    Multiple files uploading not working in Codeigniter

  8. 8

    Multiple files uploading not working in Codeigniter

  9. 9

    Error Uploading File with AJAX in codeigniter

  10. 10

    error uploading image to directory

  11. 11

    Error uploading image

  12. 12

    error uploading image to directory

  13. 13

    Error uploading image

  14. 14

    I am getting empty array, I need array of image while uploading multiple images in Codeigniter

  15. 15

    Uploading Image in db using Codeigniter 3.0.1

  16. 16

    Uploading Image in db using Codeigniter 3.0.1

  17. 17

    facing problems uploading image in a form using codeigniter

  18. 18

    uploading image in swift with multiple parameters

  19. 19

    Error uploading image using php

  20. 20

    Error in image uploading and renaming in php

  21. 21

    Error after uploading codeigniter website to server

  22. 22

    Multiple image upload error when uploading more than 20 images or so

  23. 23

    Error (Invalid argument supplied for foreach()) While uploading multiple image using php

  24. 24

    Media uploader "uploading" multiple instances of the same image

  25. 25

    Media uploader "uploading" multiple instances of the same image

  26. 26

    Uploading multiple image in yii2

  27. 27

    Codeigniter Uploading excel and Saving to database (Network Error (tcp_error))

  28. 28

    How to validate image aspect ratio before uploading in codeigniter

  29. 29

    Uploading an image of any format and showing it irrespective of the extension codeigniter

HotTag

Archive