CI 多文件上传

CI 多文件上传的时候遇到了一个问题

报错  Illegal offset type in isset or empty 


这是我写的代码 :


html 


<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>意见反馈</title>
</head>
<body>
<form action="http://www.saasapp.com/Api/SuggestionList/index" method="post" name = '' enctype="multipart/form-data" class="form-horizontal">
    <div class="form-group">
        <label class="col-lg-2 col-sm-2 control-label">意见与建议</label>
        <div class="col-lg-4">
            <input type="text" id="info" name="info" multiple/>
        </div>
    </div>
    <div class="form-group">
        <label class="col-lg-2 col-sm-2 control-label">上传图片</label>
        <div class="col-lg-4">
            <input type="file" name="photo[]">
            <input type="file" name="photo[]">
            <input type="file" name="photo[]">
            <input type="file" name="photo[]">
        </div>
    </div>
    <div class="form-group">
        <label class="col-lg-2 col-sm-2 control-label">手机号</label>
        <div class="col-lg-4">
            <input type="text" id="phone" name="phone" multiple/>
            <input type="hidden" name="uid" id="uid" value="111">
        </div>
    </div>
    <div class="form-group">
        <div class="col-lg-offset-2 col-lg-4">
            <button type="submit" name="submit" class="btn btn-default">确定</button>
        </div>
</form>
</body>
</html>

php  控制器 


public function index()
{
    // -------this-start--------   改了CI upload 方法;

    
    $imgs = $_FILES;//test
    
    
    $tmpfiles = [];
    if(!empty($imgs)) {
        //  ---test--start-----
        foreach ($imgs as $k => $v) {
            $len = count($v);
            for ($i = 0; $i <= $len-2; $i++) {
                $tmpfiles[$i] = [
                    'name' => $v['name'][$i],
                    'type' => $v['type'][$i],
                    'tmp_name' => $v['tmp_name'][$i],
                    'error' => $v['error'][$i],
                    'size' => $v['size'][$i],
                ];
            }
        }
        //  ---test--end-----

        foreach ($tmpfiles as $k => $imgval){

            $path = './static/upload';
            $path=trim($path,"/")."/";
            $Y=date("Y");
            $m=date("m");
            $d=date("d");
            if(!is_dir($path.$Y)){mkdir($path.$Y);}
            if(!is_dir($path.$Y."/".$m)){mkdir($path.$Y."/".$m);}
            if(!is_dir($path.$Y."/".$m."/".$d)){mkdir($path.$Y."/".$m."/".$d."/");}
            $upload_path =  $path.$Y."/".$m."/".$d."/";

            $config['upload_path'] = $upload_path;
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = 2048;
            $config['max_width'] = 1500;
            $config['max_height'] = 1000;
            $config['file_name'] = md5((date("YmdHis",time()))).rand();
            $this->load->library('upload', $config);
            if (!is_dir($config['upload_path'])) {
                mkdir($config['upload_path'], 0777, true);
            }
            if (!$this->upload->do_upload($imgval)) {
                if ($imgval['size'] > $config['max_size']) {
                     return Tool::returnJson(-1, '图像上传超过2M');
                }
                $error = array('error' => $this->upload->display_errors());
                return Tool::returnJson(0, '图像上传失败');

            } else {
                $data = array('upload_data' => $this->upload->data());
                if (!empty($data['upload_data'])) {
                    $new_imginfo = '/upload/'.$Y."/".$m."/".$d."/".$data['upload_data']['file_name'];
                    $getInfo['img'][] = $new_imginfo = substr($new_imginfo, 0, -1);// 上传成功 
                }
            }
        }
    }
    $this->load->model('SuggestionlistModel');
    $res = $this->SuggestionlistModel->SuggestionAdd($getInfo);
    $getInfo=[];
    if($res){
        return Tool::returnJson(1 , '添加成功',$getInfo);
    }else{
        return Tool::returnJson(-1 , '添加失败',$getInfo);
    }

    // -------this-end--------
}

CI 报错 

A PHP Error was encountered

Severity: Warning

Message: Illegal offset type in isset or empty

Filename: libraries/Upload.php

Line Number: 377



将 Upload.php 添加 是否为数组判断,并验证参数是否正确


修改后为 :

// If $field is a array Check compliance rules and use it
   if(is_array($field)){
          if (array_key_exists("name",$field)
              && array_key_exists("type",$field)
              && array_key_exists("tmp_name",$field)
              && array_key_exists("error",$field)
              && array_key_exists("size",$field))
          {$_file = $field;}
          else {
              return FALSE;
          }
      }
// Is $_FILES[$field] set? If not, no reason to continue.
elseif (isset($_FILES[$field]))
{
   $_file = $_FILES[$field];
}

好了 ,可以用了。

在网上没搜到相关示例,不知道有人也遇到过类似问题,有没有不动CI 框架,用更好的方法解决呢。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值