Form Regular Expression verification and file upload verification, form File Upload Verification

Source: Internet
Author: User

Form Regular Expression verification and file upload verification, form File Upload Verification

Form Regular Expression verification is mainly used to filter form submission information and prevent SQL injection (such as logon interface). uploaded files also need to be verified by the filename suffix and size, below 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 'uploaded successfully';} if (! Preg_match ("/^ [\ x {4e00}-\ x {9fa5}] + $/u", $ user )) {// check whether the user name is composed of Chinese characters. echo "the user name can only consist of Chinese characters! "; Die;} else if (! Preg_match ("/^ [\ x {4e00}-\ x {9fa5} A-Za-z0-9 _] + $/u", $ password )) {// Regular Expression check if the password contains invalid characters. The echo password cannot contain special characters! '; Die;} else if (! In_array (pathinfo ($ file, PATHINFO_EXTENSION), $ arr) {echo "incorrect file format"; die;} else {echo 'allow registration! ';}

With common php Regular Expressions:

Match China zip code: [1-9] \ d {5 }(?! \ D)

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

Matched IP Address: \ d + \. \ d +

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

The regular expression matching the Email address: \ w + ([-+.] \ w +) * @ \ w + ([-.] \ w + )*\. \ w + ([-.] \ w + )*

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


Preg_match (): the first parameter is a regular rule, the second is a verified string, and a Boolean value is returned.

Preg_replace (): replace the characters that match the regular rules in a string.

The above section describes the form Regular Expression verification and file upload verification functions. I hope this will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.