表单
<form action="/create" method="post" enctype="multipart/form-data">
@csrf
<p>学员名称:<input type="text" name="username"></p>
<p>学员年龄:<input type="text" name="number"></p>
<p>学员性别:
<input type="radio" name="xin" value="男" checked>男
<input type="radio" name="xin" value="女">女
</p>
<p>上传图片:<input type="file" name="log"></p>
<button type="submit">提交</button>
</form>
控制器
public function create(Request $request){
$param=$request->post();
if($request->hasFile('log') && $request->file('log')->isValid()){
// 对上传的文件进行必要的处理
$filename=date('ymdHis').rand(1000,9999).'.'.$request->file('log')->extension();
// 移动到指定目录下
$request->file('log')->move('./statics/upload',$filename);
}
//替换属性
$data=[
'name'=>$param['username'],
'age'=>$param['number'],
'sex'=>$param['xin'],
'image'=>'/statics/upload/'.$filename
];
User::create($data);
echo "添加成功";
}
图片会保存到本地public/statics/upload下?
|