Php judges the file upload type and filters unsafe data. _ PHP Tutorial

Source: Internet
Author: User
Php judges the file upload type and filters insecure data ,. Php judges the file upload type and the method of filtering unsafe data. This article describes how php judges the file upload type and filters unsafe data. Share it with you for your reference. Php is used to determine the file upload type and filter insecure data,

This article describes how to use php to determine the file upload type and filter insecure data. Share it with you for your reference. The details are as follows:

It is not allowed to upload files other than image files. if you are prompted, do not obtain the file extension to determine the type. this is the least secure, we use $ _ FIlES ['form'] ['type'].

This can read the file content to identify the file type, but it can recognize limited, but if you use an image, it is enough to understand. function, filter unsafe characters, the instance function code is as follows:

The code is as follows:

Function s_addslashes ($ string, $ force = 0 ){
If (! Get_magic_quotes_gpc ()){
If (is_array ($ string )){
Foreach ($ string as $ key => $ val ){
$ String [$ key] = s_addslashes ($ val, $ force );
}
} Else {
$ String = str_replace ("& # x", "& # x", $ string );//

// Filter unsafe characters
$ String = addslashes ($ string );
}
}
Return $ string;
}

// Usage example:
$ _ COOKIE = c_addslashes ($ _ COOKIE );
$ _ POST = c_addslashes ($ _ POST );
$ _ GET = c_addslashes ($ _ GET );

// Add a public file
If ($ _ FILES ){
Foreach ($ _ FILES as $ key =>$ _ value)
{
$ _ FILES [$ key] ['type'] = $ _ value ['type'];
}
If (substr ($ _ FILES [$ key] ['type'], 0, 6 )! = 'Image /')
{
Exit;
}
}

I hope this article will help you with PHP programming.

Example: This article describes how php judges the file upload type and filters insecure data. Share it with you for your reference. With...

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: info-contact@alibabacloud.com 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.