ThinkPHP3.2.2-多文件上传

ThinkPHP3.2.2-多文件上传

==》在项目目录创建/public/Uploads;
这里写图片描述

创建控制器FileController.class.php

<?php
namespace Home\Controller;
use Think\Controller;
class FileController extends Controller {
    public function index(){
    //视图
        $this->display('file/index');
    }
    public function images(){
        $model=D('File');
        $img=$_FILES['images'];
        // $res=$model->getImg($img);  单文件上传
        $res=$model->getImgs($img);  //多文件上传
        print_r($res);die;
    }
}

创建模型FileModel.class.php

<?php  
namespace Home\Model;
use Think\Model;
class FileModel extends Model {
    //单文件上传
    public function getImg($img){    
        $upload = new \Think\Upload();
        $upload->maxSize   =     3145728 ;
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');
        $upload->savePath  =      './Public/Uploads/';
        $upload->rootPath  =      './';
        $info   =   $upload->uploadOne($img);
        $img_path=$info['savepath'].$info['savename'];
        return $img_path;
    }
    //多文件上传
    public function getImgs($img){
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->savePath  =      './Public/Uploads/';
        $upload->rootPath  =      './';
        $info   =   $upload->upload();
        if(!$info) {// 上传错误提示错误信息    
        $this->error($upload->getError());
        }else{// 上传成功 获取上传文件信息    
            $arr=array();
            foreach($info as $file){        
                $arr[]=$file['savepath'].$file['savename'];    
            }
        }
        return $arr;
    }
}
?>

创建视图/View/file/index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<center>
    <form action="{:U('File/images')}" method= "post" enctype ="multipart/form-data">
    //多文件
        <input type="file" name="img[]" multiple='multiple'>
        <input type="submit" value="Upload">
    </form>
</center> 
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值