Codeigniter中的多个图像上传错误

时间:2017-01-13 08:23:04

标签: image codeigniter file-upload

我正在尝试通过我的表单上传多个图片。我已经照顾了许多人期望的form_open_multipart,在上传输入中将名称称为Array。

我对图片上传没有任何问题。我可以成功上传多张图片,但是当我选择包含图片的任何文字或非图片文件时,就会出现问题。

我正在检查如果用户将其混淆(意外地选择带有图像的任何其他文件)图像与文本文件然后一些文件上传然后我得到错误类型的文件的错误的情况。 在理想情况下,如果任何一个文件没有正确的文件类型,它可能不允许上传文件。

所以任何人都可以帮助我,在文件上传之前如何检查是否允许所有文件类型?

我尝试了很多例子,但总共遇到了同样的问题。

如果我上传多张图片没有问题。它工作正常。

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

以下是我的控制器

$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 个答案:

答案 0 :(得分:1)

第一个问题主要取决于您如何处理错误。

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

上面的代码将简单地停止for循环的其余部分并重定向第一个错误。

一个简单的解决方案是将其更改为记录失败并继续处理:

$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');
}

或者,您需要根据文件扩展名查看手动验证文件。

$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');
}

希望有所帮助