如何在Codeigniter 3.0.1中上传多个文件 [英] How to upload multiple files in codeigniter 3.0.1

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

问题描述

如何在codeigniter 3.0.1中上载多个文件.堆栈溢出中也有类似的问题和解决方案,但不幸的是,没有一个问题和解决方案正在帮助解决我面临的问题.

How to upload multiple files in codeigniter 3.0.1. There are similar issues and solutions in stackoverflow but unfortunately non of them are helping to fix the issue I am facing.

这是出现在You did not select a file to upload并带有我当前代码的错误消息

This is the error message appearing You did not select a file to upload with my current code

视图(addGallery)

<section>
    <h2>Add Gallery</h2>
        <?php echo form_open('Newsupload/gallery', ['id'=>'news', 'name'=>'news', 'method'=>'post','enctype'=>'multipart/form-data']) ?>

        <div class="grp width-50">
            <label for="name">Album Name</label>
            <input type="text" name="name" id="name" value="" placeholder="">
        </div>
        <div class="grp width-100">
            <div id="selectedFiles"></div>
            <input type="file" id="files" name="files[]" multiple size="20"><br/>
        </div>
        <?php if (isset($error)) {
            echo $error;
        } ?>
        <grp class="grp width-100">
            <button>Add</button>
        </grp>
    </form>
</section>

控制器(图库)

public function gallery()
{

    $this->load->library('upload');

    $files = $_FILES;
    $cpt = count($_FILES['files']['name']);
    for($i=0; $i<$cpt; $i++)
    {           
        $_FILES['files']['name']= $files['files']['name'][$i];
        $_FILES['files']['type']= $files['files']['type'][$i];
        $_FILES['files']['tmp_name']= $files['files']['tmp_name'][$i];
        $_FILES['files']['error']= $files['files']['error'][$i];
        $_FILES['files']['size']= $files['files']['size'][$i];    

        $this->upload->initialize($this->set_upload_options());
        // $this->upload->do_upload('files[]');
        if (!$this->upload->do_upload('files[]'))
        {  
            $error =['error' => $this->upload->display_errors()];
            $this->load->view('admin/addGallery', $error);
        }
    }
}
public function set_upload_options()
{
    $config['upload_path'] = getcwd().'/upload/';
    $config['allowed_types'] = 'gif|jpg|jpeg|png';
    $config['remove_spaces'] = true;
    return $config;
}

推荐答案

默认情况下,codeIgniter不支持多文件上传.所以你可以使用 此库 CodeIgniter多个上传库

By default codeIgniter doesn't support multi-file upload. So you can use this library CodeIgniter Multiple Upload Library

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

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