Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the function of form regular Verification and File upload Verification by php

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

这篇文章主要介绍php如何实现表单正则验证及文件上传验证功能,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

表单正则验证主要是用来对表单提交信息的过滤,防止sql注入(比如登录界面),上传的文件也需要进行文件名后缀和大小进行验证,下面是一个简单的表单验证

header("Content-type:text/html;charset=utf-8"); $user = isset($_POST['user'])?$_POST['user']:null; $password = isset($_POST['password'])?$_POST['password']:null; $arr = array('png','gif','jpg'); $uploads = move_uploaded_file($_FILES['face']['tmp_name'],'uploads/'.$_FILES['face']['name']); $file = 'uploads/'.$_FILES['face']['name']; if($uploads){ echo '上传成功'; } if(!preg_match("/^[\x{4e00}-\x{9fa5}]+$/u", $user)){ //正则检查用户名是否为全汉字组成 echo "用户名只能由纯汉字组成!"; die; }else if(!preg_match("/^[\x{4e00}-\x{9fa5}A-Za-z0-9_]+$/u",$password)){ //正则检查密码是否含有非法字符 echo '密码不能包含特殊字符!'; die; }else if(!in_array(pathinfo($file, PATHINFO_EXTENSION),$arr)){ echo "文件格式不正确"; die; }else{ echo '允许注册!'; }

附常用php正则表达式:

匹配中国邮政编码:[1-9]\d{5}(?!\d)

匹配身份证:\d{15}|\d{18}

匹配ip地址:\d+\.\d+\.\d+\.\d+

匹配网址URL的正则表达式:[a-zA-z]+://[^\s]*

匹配Email地址的正则表达式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*

匹配中文字符的正则表达式: [\u4e00-\u9fa5]

函数:

preg_match():第一个参数为正则规则,第二个为被验证的字符串,返回布尔值

preg_replace ():对一个字符串中附和正则规则的字符进行字符替换

以上所述是小编给大家介绍的表单正则验证及文件上传验证功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

以上是"php如何实现表单正则验证及文件上传验证功能"这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report