网站中很多表单都会用到上传图片,logo,照片,用户也会上传图片,这个时候网站就需要一个上传图片的功能,而且在上传后希望能预览一下看上传的对不对。
thinkphp5加layui实现图片上传功能(带图片预览)思路,异步传输图片并预览,将异步上传后的值返回表单隐藏域再提交。
1、引入文件
首先,要引入jQuery文件,这是必须的
部分功能简介:商品收藏夹功能热门商品最新商品分级价格功能自选风格打印结算页面内部短信箱商品评论增加上一商品,下一商品功能增强商家提示功能友情链接用户在线统计用户来访统计用户来访信息用户积分功能广告设置用户组分类邮件系统后台实现更新用户数据系统图片设置模板管理CSS风格管理申诉内容过滤功能用户注册过滤特征字符IP库管理及来访限制及管理压缩,恢复,备份数据库功能上传文件管理商品类别管理商品添加/修改/
2、HTML部分
3、功能实现
4、后台处理
图片上传
public function upload_img(){
$file = request()->file('file');
if($file==null){
exit(json_encode(array('code'=>1,'msg'=>'没有文件上传')));
}
$info = $file->move(ROOT_PATH.'public'.DS.'uploads');
$ext = ($info->getExtension());
if(!in_array($ext,array('jpg','jpeg','gif','png'))){
exit(json_encode(array('code'=>1,'msg'=>'文件格式不支持')));
}
$img = '/uploads/'.$info->getSaveName();
exit(json_encode(array('code'=>0,'msg'=>$img)));
}保存内容
public function save(){
$id = (int)input('post.id');
$data['title'] = trim(input('post.title'));
$data['channel_id'] = (int)input('post.channel_id');
$data['charge_id'] = (int)input('post.charge_id');
$data['area_id'] = (int)input('post.area_id');
$data['img'] = trim(input('post.img'));
$data['url'] = trim(input('post.url'));
$data['keywords'] = trim(input('post.keywords'));
$data['desc'] = trim(input('post.desc'));
$data['status'] = (int)input('post.status');
if($data['title'] == ''){
exit(json_encode(array('code'=>1,'msg'=>'影片名称不能为空')));
}
if($data['url'] == ''){
exit(json_encode(array('code'=>1,'msg'=>'影片地址不能为空')));
}
if($id){
$this->db->table('video')->where(array('id'=>$id))->update($data);
}else{
$data['add_time'] = time();
$this->db->table('video')->insert($data);
}
exit(json_encode(array('code'=>0,'msg'=>'保存成功')));
}










