从前端上传图片
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="/demo/public/index.php/index/tiaozhuan1/uploads" enctype="multipart/form-data" method="post">
//action后面是上传之后跳转的页面方法
<input type="file" name="image"/> <br> <br> <br>
<input type="submit" value="上传" />
</div>
</body>
</html>
控制器上的方法(即上传到数据库)
public function uploads(){
//获取表单上传文件 例如上传了001.jpg
$file = request()->file('image');
//var_dump($file);
// 移动到框架应用根目录/public/uploads/ 目录下
if($file){
$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
$data['pic']=$info->getSaveName();
if(Db::name('picture')->insert($data)){
return $this->success('上传成功','tupian');}
else{
return $this->error('上传失败!');}
}
return $this->fetch();
}
在浏览器上显示图片
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
{volist name='tupian' id='vo'}
<div><img src="__IMG__{$vo.pic}"></div>
{/volist}
</body>
</html>