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

[图片处理] 如何上传一个数组文件

[复制链接]
发表于 2017-11-3 10:23:00 | 显示全部楼层 |阅读模式
上传图片表单名称 以数组命名<div class="file_arr">
    <div class="img_file">
        <input id="cus_img" name="cus_img[]" onchange="c('cus_img','show')" type="file"><br>
        <img src="" id="show" width="200">
    </div>
    <div class="img_file">
        <input id="cus_img1" name="cus_img[]" onchange="c('cus_img1','show1')" type="file"><br>
        <img src="" id="show1" width="200">
    </div>
    <div class="img_file">
        <input id="cus_img2" name="cus_img[]" onchange="c('cus_img2','show2')" type="file"><br>
        <img src="" id="show2" width="200">
    </div>
    <div class="img_file">
        <input id="cus_img3" name="cus_img[]" onchange="c('cus_img3','show3')" type="file"><br>
        <img src="" id="show3" width="200">
    </div>
    <div class="img_file">
        <input id="cus_img5" name="cus_img[]" onchange="c('cus_img5','show5')" type="file"><br>
        <img src="" id="show5" width="200">
    </div>
</div>

封装好的函数

public function file_do_upload($filename,$name='',$w=800,$h=600)
{
    $path = 'uploads/'.date('Y-m-d',time()).'/';    //上传文件的目录
    //判断文件目录是否存在,不存在则创建
    if(!is_dir($path)){
        mkdir($path,0777,true);
    }
    $config['upload_path']      = $path;        //存放文件的目录
    $config['allowed_types']    = 'gif|jpg|png|bmp|jpeg';  //上传文件的类型
    $config['max_size']     = 12000;  //上传文件的大小
    $config['quality']     = 22;  //上传文件的大小
    $config['file_name'] = time() . mt_rand(1000,9999).$name;  //修改上传的文件名
    // 载入上传类
    $this->upload->initialize($config);
    // 执行上传
    $this->upload->do_upload($filename);
    // 返回信息
    $info = $this->upload->data();
    //配置缩略图信息
    $arr['image_library']      = 'GD2';
    $arr['source_image']      = $info['full_path'];
    $arr['create_thumb']    = FALSE;             //开启缩略图功能并保存原图
    $arr['maintain_ratio']     = TRUE;          //使图像保持原始的纵横比例进行缩放。
    $arr['width']        = $w;
    $arr['height']       = $h;
    //载入缩略图类
    $this->load->library('image_lib');
    $this->image_lib->initialize($arr);       //重复使用函数时 重载缩略图配置
    //执行动作
    $this->image_lib->resize();
    $file_path = $path.$info['file_name'];

    return $file_path;
}

在控制器接收


$imgarr = $_FILES['cus_img']['name'];
$ar = count(array_filter($imgarr));for($i=1;$i<=$ar;$i++)
{
    echo '<pre>';
    print_r($imgarr);
    print_r($this->pufun->file_do_upload('cus_img[]'));
}

这个写法 没有成功获取到上传文件   可以打印出数据 但 只有文件名 没有 后缀  
结果如下
Array(    [0] =>     [1] => zyc.png    [2] =>     [3] => )uploads/2017-11-03/15096737819438

不是以数组 形式命名  直接 cus_img  可以 成功获取数据  并上传图片,







发表于 2017-11-3 14:04:48 | 显示全部楼层
上传类不支持传多个文件。

本版积分规则