前台模版页:view/staff/demo2
{load href="/static/bootstrap/css/bootstrap.css"}
<div class="container">
<div class="row">
<h3 class="text-center">员工信息表</h3>
<div class="col-md-8 col-md-offset-2">
<table class="table table-bordered table-hover text-center">
<tr class="info">
<td>ID</td>
<td>姓名</td>
<td>性别</td>
<td>年龄</td>
<td>工资</td>
</tr>
{empty name="staffs"}
<h3>当前数据为空</h3>
{else /}
{volist name="staffs" id="staff"}
<tr>
<td>{$staff.staff_id}</td>
<td>{$staff.name}</td>
<td>
{//$staff.sex}
{in name="staff.sex" value="0,1"}
{if $staff.sex==0}
男
{else /}
女
{/if}
{/in}
</td>
<td>{//$staff.age}
{between name="staff.age" value="20,30"}
青年
{/between}
{between name="staff.age" value="31,40"}
中年
{/between}
</td>
<td>{$staff.salary}</td>
</tr>
{/volist}
{/empty}
</table>
<div class="text-right">{$page|raw}</div>
</div>
</div>
</div>
{load href="/static/jquery/jquery-3.2.1.min.js"}
{load href="/static/bootstrap/js/bootstrap.js"}
上传模版页 view/staff/demo3 (form:enctype="multipart/form-data" 很重要)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>文件上传</title>
</head>
<body>
<h4>文件上传</h4>
<form action={:url('demo4')} method="post" enctype="multipart/form-data">
<input type="file" name="file" id="">
<input type="submit" value="上传">
</form>
</body>
</html>
数据模型:index/model/staff
<?php
/**
* @Author: owlcity
* @Date: 2018-05-31 09:47:14
* @Last Modified by: owlcity
* @Last Modified time: 2018-05-31 09:48:57
*/
namespace app\index\model;
use think\Model;
class Staff extends Model{
protected $table = 'staff';
protected $pk = 'staff_id';
}
控制器:controller/index/staff
<?php
/**
* @Author: owlcity
* @Date: 2018-05-31 09:49:40
* @Last Modified by: owlcity
* @Last Modified time: 2018-05-31 15:41:12
*/
namespace app\index\controller;
use think\Controller;
use app\index\model\Staff as StaffModel;
use think\facade\Request;
class Staff extends Controller{
//循环标签
public function demo1()
{
$staffs = StaffModel::all(function($query){
$query->field(['staff_id','name','sex','age','salary']);
// ->where('salary < 1000');
});
// echo "1";
$this->view->assign('staffs', $staffs);
return $this->view->fetch();
}
// 分页
public function demo2(){
$config = [
'type' => 'bootstrap',
'var_page' => 'page'
];
$num = 5;
// 是否简单分页
$simple = false;
$paginate = StaffModel::paginate($num,$simple,$config);
$page = $paginate->render();
// 模版数据传值
$this->view->assign('staffs',$paginate);
$this->view->assign('page',$page);
return $this->view->fetch();
}
// 文件上传
public function demo3(){
return $this->view->fetch();
}
public function demo4(){
// 1:获取文件信息
$file = Request::file('file');
if(is_null($file)){
$this->error("没有选择任何文件");
}
// dump($file);
// 2:移动到服务器上的指定目录 public/uploads
// 如果上传成功返回一个已经赋值的文件对象,失败返回false;
// $res = $file->validate(['ext'=>'jpg,jpeg,png'])->move('uploads','');
$rule = ['size'=>2097152,'ext'=>'jpg,jpeg,png,gif','type'=>'image/jpeg,image/png,image/gif'];
// $res = $file->check($rule);
if($file->check($rule)) {
$fileInfo = $file->move('uploads');
// $fileInfo = $file->move('uploads','');//如果使用原文件名 第二个参数为空
$res = '<h3 style="color:green;">上传成功</h3>文件名是:'.$fileInfo->getSaveName();
} else {
$res = '<h3 style="color:red;">上传失败</h3>'.$file->getError();
}
return $res;
// // 3:对上传文件验证,大小/类型
// if($res == false){
// $this->error($file->getError());
// }
// $this->success("上传成功");
}
}