SoFunction
Updated on 2025-03-03

Form regular verification and file upload verification functions

Form regular verification is mainly used to filter form submission information to prevent SQL injection (such as login interface). Uploaded files also need to verify file name suffix and size. The following is a simple form verification.

 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 ‘Upload successfully‘;
 }
 if(!preg_match("/^[\x{4e00}-\x{9fa5}]+$/u", $user)){
   //Check whether the user name is composed of all Chinese characters   echo "Usernames can only be composed of pure Chinese characters!";
   die;
 }else if(!preg_match("/^[\x{4e00}-\x{9fa5}A-Za-z0-9_]+$/u",$password)){
   //Check the password to contain illegal characters   echo ‘Passwords cannot contain special characters!‘;
   die;
 }else if(!in_array(pathinfo($file, PATHINFO_EXTENSION),$arr)){
   echo "File format is incorrect";
   die;
 }else{
   echo ‘Registration allowed!‘;
 } 

Attached is a common php regular expression:

Match the Chinese postal code: [1-9]\d{5}(?!\d)

Match ID card: \d{15}|\d{18}

Match IP address: \d+\.\d+\.\d+\.\d+

Regular expression matching URL: [a-zA-z]+://[^\s]*

Regular expression matching email address: \w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*

Regular expression matching Chinese characters: [\u4e00-\u9fa5]

function:

preg_match(): The first parameter is a regular rule, the second is a string to be verified, returning a boolean value

preg_replace (): Character replacement for characters that are echoed with regular rules in a string

The above is the form regular verification and file upload verification functions introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!