用户
 找回密码
 入住 CI 中国社区
搜索
查看: 12498|回复: 5
收起左侧

[库 Library] CI 多文件上传 Multiple upload

[复制链接]
发表于 2013-7-17 16:58:53 | 显示全部楼层 |阅读模式
今天在处理CI多文件上传时,需要用到多文件上传,看到有帖子使用 swfupload 上传方式,发现并不适合我的需求的(当上传的文件需要和表单数据一同提交时,swfupload 上传就不适合了),抓狂。也有看到一些文章但都是很老的了,于是写了个基于CI2.13的版本.
贴代码:
PHP复制代码
 
        /**
         * Upload more file
         *
         * $param       string  field
         * @return      array
         */

        public function multiple($field)
        {
                // Is $_FILES[$field] set? If not, no reason to continue.
                if ( ! isset($_FILES[$field]))
                {
                        $this->set_error('upload_no_file_selected');
                        return FALSE;
                }
 
                // 临时文件上传数组,用于整合自己想要的形式
                $tmpfiles       = array();
                for ($i = 0, $len = count($_FILES[$field]['name']); $i < $len; $i ++)
                {
                        if ($_FILES[$field]['size'][$i])
                        {
                                $tmpfiles['_SR_' . $i] = array(
                                                'name'  => $_FILES[$field]['name'][$i],
                                                'type'  => $_FILES[$field]['type'][$i],
                                                'tmp_name'      => $_FILES[$field]['tmp_name'][$i],
                                                'error' => $_FILES[$field]['error'][$i],
                                                'size'  => $_FILES[$field]['size'][$i],
                                        );
                        }
                }
 
                //覆盖 $_FILES 内容
                $_FILES = $tmpfiles;
 
                $errors = array();
                $files  = array();
                $index  = 0;
                $_tmp_name      = preg_replace('/(.[a-z]+)$/', '', $this->file_name);
                foreach ($_FILES as $key => $value)
                {
                        /*
                         * 多文件上传的命名规则,用于替代CI中自由的文件命名方式
                         *
                         * -SR-17-50557-0.jpg
                         * -SR-17-50557-1.jpg
                         * -SR-17-50557-2.jpg
                         */

                        $this->_file_name_override      = $_tmp_name . '-' . $index;
                        if( ! $this->do_upload($key))
                        {
                                $errors[$index] = $this->display_errors('', '');
                                $this->error_msg        = array();
                        }
                        else
                        {
                                $files[$index] = $this->data();
                        }
                        $index  ++;
                }
                // 返回数组
                return array(
                                'error' => $errors,
                                'files' => $files
                        );
        }
 
复制代码

将此代码贴在 system\libraries\Upload.php 里即可,也可写扩展在你的核心中 \application\libiraries\MY_Upload.php中
我是直接将system\libraries\Upload.php粘到\application\libiraries\Upload.php
本人试了,挺好。
不好意思说干了好几年的PHP··· 如有赞助空间更佳,因为敝人想要建个博客。。。blog.stepreal.com 没有空间

原始需求图

原始需求图

原始需求图

原始需求图

评分

参与人数 1威望 +5 收起 理由
Hex + 5 很给力!

查看全部评分

发表于 2013-9-11 13:35:30 | 显示全部楼层
{:soso__17615529269517227981_3:}
发表于 2014-5-11 14:32:02 | 显示全部楼层
菜鸟的悲哀是有了代码不会用,能举个例子吗
发表于 2014-5-12 10:46:09 | 显示全部楼层
这个在原生的php里面就有的吧,不在ci里面也可以用,。。
发表于 2016-12-20 10:06:46 | 显示全部楼层
有没有写多图片上传完善一点的

本版积分规则