控制器里
public function add()
{
$file = request()->file('img');
if (empty($file)) {
return alert('您好,请上传照片!','http://www.xiaosan.com/tp5/public/index.php/index/backstage/index',6,3);
}
//移动到框架应用根目录/public/uploads/ 目录下
$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
if ($info) {
//获取当前保存路径
$url = $info->getSaveName();
//接收当前表单数据
$data = $_POST;
$data['img'] = $url;
$res=Db('qname')->insert($data);
if($res){
return alert('提交成功!','http://www.xiaosan.com/tp5/public/index.php/index/backstage/show',6,3);
}else{
return alert_error('提交失败!','http://www.xiaosan.com/tp5/public/index.php/index/backstage/index',6,3);
}
} else {
//上传失败获取错误信息
$this->error($file->getError());
}
}
表单页面
<!DOCTYPE html>
<html lang="en"><head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="/tp5/layer/layer/jquery-1.8.3.js"></script>
<style>
body{
background-image:url("../../../img/16.jpg");
background-size:60% ;
/*width:100%;
height: 100%;*/
font-family: 'Oleo Script', cursive;
}
#lg-form > div {
margin:20px 5px;
padding:5px 0;
}
#lg-form label{
font-size: 25px;
line-height: 15px;
font-color:red;
}
#lg-form input[type="text"],
#lg-form input[type="file"]{
border:2px solid rgba(51,51,51,.5);
-webkit-border-radius:20px;
-moz-border-radius:20px;
padding: 5px;
font-size: 16px;
line-height: 25px;
width: 20%;
font-family: 'Oleo Script', cursive;
text-align:center;
}
#lg-form div:nth-child(3) {
text-align:center;
}
#lg-form button{
font-family: 'Oleo Script', cursive;
font-size: 18px;
border:1px solid pink;
padding:5px 10px;
border:1px solid rgba(51,51,51,.5);
-webkit-border-radius:10px;
-moz-border-radius:10px;
-webkit-box-shadow: 2px 1px 1px #aaa;
-moz-box-shadow: 2px 1px 1px #aaa;
cursor:pointer;
}
#lg-form button:active{
-webkit-box-shadow: 0px 0px 1px #aaa;
-moz-box-shadow: 0px 0px 1px #aaa;
}
#lg-form button:hover{
background:pink;
}
#message{width:100%;text-align:center}
.success {
color: green;
}
.error {
color: red;
}
</style>
</head>
<body>
<center>
<h1>球员信息</h1>
<form action="{:url('backstage/add')}" id="lg-form" name="lg-form" method="post" enctype="multipart/form-data">
<div>
<label for="name">球员名称:</label>
<input type="text" name="name" placeholder="name"/>
</div>
<div>
<label for="img">球员照片:</label>
<input type="file" name="img" placeholder="photo" />
</div>
<div>
<label for="age">球员年龄:</label>
<input type="text" name="age" placeholder="age" />
</div>
<div>
<label for="num">所属球队:</label>
<input type="text" name="num" placeholder="team" />
</div>
<div>
<label for="text">个人简历:</label>
<textarea rows="10" cols="30" name="text" class="text">
</textarea>
</div>
<div>
<button type="submit" id="login" class="login">提交</button>
</div>
</form>
<div id="message"></div>
</div>
</center>
</body>
</html>