Codeigniter中的多个图像上传错误 [英] Multiple image uploading error in Codeigniter

查看:147
本文介绍了Codeigniter中的多个图像上传错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过我的表单上传多个图片。我已经照顾了许多像form_open_multipart期待,在上传输入中调用一个名称为数组。



我没有问题的图片上传。我可以成功上传多张图片,但问题出在我选择任何文字或非图片文件的图像。

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



所以任何人可以帮助我,我怎么才能在文件上传之前检查所有文件是否允许输入或不是?

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



如果我上传了多张图片,没有任何问题。这工作正常。

 < input type =filename =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;
//初始化上传类
$ this->加载 - >库('upload',$ config);

$ upload_error = array();
$ b $($ 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'=> $这 - > upload->的display_errors());
// $ this-> load-> view('multiple_upload_view',$ upload_error);

$ this-> session-> set_flashdata('upload_error',$ upload_error);
重定向('property_master / view_prop_master_form');

break;


$ / code $ / pre

解决方案

问题主要在于如何处理错误。

  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);
重定向('property_master / view_prop_master_form');

break;





$ b上面的代码将简单地停止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;
//初始化上传类
$ this->加载 - >库('upload',$ config);

$ upload_error = array();
$ b $($ 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()); ($ upload),



















$ ;
重定向('property_master / view_prop_master_form');

$ / code>

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

  $ config ['upload_path'] ='./assets/prop_pic/'; 
$ config ['allowed_types'] ='gif | jpg | png';
$ config ['max_size'] = 20000;
$ config ['encrypt_name'] = TRUE;
$ config ['detect_mime'] = TRUE;
//初始化上传类
$ this->加载 - >库('upload',$ config);

$ upload_error = array();
$ b 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>无效的文件扩展名(%s)< / p>',$ ext));
继续;
}

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

if(!$ this-> upload-> do_upload('userfile')){
$ upload_error [$ key] = array('error'=> $ this- > upload->的display_errors());
} else {
//用$ this-> upload-> data()做一些事情; ($ upload),



















$ ;
重定向('property_master / view_prop_master_form');
}

希望有帮助


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

解决方案

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

这篇关于Codeigniter中的多个图像上传错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆