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>