PHP to determine the type of file upload and filter unsafe data methods, _php tutorial

Source: Internet
Author: User
Tags php file upload

PHP to determine the type of file upload and filter unsafe data methods,


In this paper, the method of PHP judging file upload type and filtering unsafe data is described. Share to everyone for your reference. Specific as follows:

Do not upload files other than picture files, prompt, do not get the file name extension to determine the type, this is the most insecure, we use $_files[' form ' [' type '].

This can read the file content to identify the file type, but it can be recognized limited, but if you use the picture is enough to understand. function, filter unsafe characters, instance function code is as follows:
Copy the Code code 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 some unsafe characters
$string = Addslashes ($string);
}
}
return $string;
}

Usage examples:
$_cookie = C_addslashes ($_cookie);
$_post = C_addslashes ($_post);
$_get = C_addslashes ($_get);

Include in the 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 is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/929089.html www.bkjia.com true http://www.bkjia.com/PHPjc/929089.html techarticle PHP to determine the type of file upload and filter unsafe data methods, this article describes the php file upload type and the method of filtering unsafe data. Share to everyone for your reference. ...

  • Related Article

    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.