您现在的位置是:首页 > 文章详情

laravel 图片/头像上传通用方法

日期:2018-08-29点击:409
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"); } }
原文链接:https://yq.aliyun.com/articles/630559
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章