1

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;
        }
    }

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks For your suggestion and help sir. I definitely apply this solution to my code. thanks again & really appreciate your support.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.