laravel 图片/头像上传通用方法
if($_FILES["file"]["error"]!==1) { return redirect()->route("change_password")->with("msg","头像上传出错 错误码:".$_FILES["file"]["error"]); } else { //没有出错 //加限制条件 //判断上传文件类型为png或jpg且大小不超过1024000B if(($_FILES["file"]["type"]=="image/png"||$_FILES["file"]["type"]=="image/jpeg")&&$_FILES["file"]["size"]<1024000) { //地址文件名拼接 $type=explode(".",$_FILES["file"]["name"]); $filename ="./upload/".$user_id.'-'.date('YmdHis',time()).mt_rand(1000,9999).'.'.$type[1]; //转码,把utf-8转成gb2312,返回转换后的字符串, 或者在失败时返回 FALSE。 $filename =iconv("UTF-8","gb2312",$filename); //检查文件或目录是否存在 if(file_exists($filename)) { return redirect()->route("change_password")->with("msg","图片已存在"); } else { $dir = './upload'; is_dir($dir) OR mkdir($dir, 0777, true); //保存文件, move_uploaded_file 将上传的文件移动到新位置 move_uploaded_file($_FILES["file"]["tmp_name"],$filename);//将临时地址移动到指定地址 if($staff['image']!=null){ if(file_exists($staff['image'])){ unlink($staff['image']); } } $staff->image=$filename; if($staff->save()){ return redirect()->route("change_password")->with("msg","头像上传成功"); } } } else { return redirect()->route("change_password")->with("msg","图片类型只能为JPG或PNG,且大小不能超过1M"); } }
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Java 面向对象 之 匿名内部类 警报器
http://www.verejava.com/?id=16992876520663 /** 知识点: 匿名内部类 题目: 摩托车上装有警报器,当有人碰到摩托车时, 警报器发出报警响声 思路: 1. 抽象出类: 1.1 摩托车(Moto),报警器(Alarm), 人(Person) 2. 找出类的关系: 2.1 摩托车上装有报警器 Alarm -> Moto(1对1) 2.2 警报器有不同品牌的分类 Alarm 3. 找出类属性: 3.1 Moto(品牌,报警器的引用) 3.2 Alarm(品牌) 3.3 Person(姓名) 4. 找出类方法: 4.1 摩托车上装有报警器 Moto{load(Alarm alarm)} 4.2 人碰到摩托车 Person{touch(Moto moto)} 4.3 警报器发出响声 Alarm{beep()} */ public class TestMoto2 { public static void main(String[] args) { //准备生产一台南方摩托车 Moto moto = new Moto("南方"); //摩托车上预装一...
- 下一篇
Laravel Response
//响应json /*$data = [ 'errCode'=>0, 'errMsg' =>'success', 'data' => 'yxh', ]; return response()->json($data);*/ //重定向 //return redirect('Hello'); //return redirect('Hello')->with('message','yxh'); //return redirect()->action('UserController@Hello')->with('message','yxh'); //return redirect()->route('Hello')->with('message','yxh'); //返回上一个页面 //return redirect()->back();
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS8编译安装MySQL8.0.19
- Windows10,CentOS7,CentOS8安装MongoDB4.0.16
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- CentOS7,CentOS8安装Elasticsearch6.8.6
- CentOS6,7,8上安装Nginx,支持https2.0的开启
- CentOS8安装Docker,最新的服务器搭配容器使用
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- CentOS7安装Docker,走上虚拟化容器引擎之路