PHP disables certain types of file uploads _ PHP Tutorial

Source: Internet
Author: User
PHP prohibits certain types of files from being uploaded. To prevent some people from uploading executable files such as exe to the server, we can write a program to determine the type of the file to be uploaded. otherwise, files that do not match the type will be rejected. To prevent some people from uploading executable files such as exe to the server, we can write a program to determine the type of the file to be uploaded. otherwise, files that do not match the type will be rejected.

The following is a PHP program that implements this function:

Function ($ file_name, $ pass_type = array ('jpg ', 'jpeg', 'GIF', 'bmp ', 'PNG ')) {// An array composed of suffixes that allow file types $ file = $ pass_type; // the suffix of the file name to be intercepted $ kzm = substr (strrchr ($ file_name ,". "), 1); // determines whether the suffix is in the array $ is_img = in_array (strtolower ($ kzm), $ file); if ($ is_img) {return true ;} else {return false ;}}
In_array () function

The in_array () function searches for the given value in the array. Usage: in_array (value, array, type ).

  • Parameter value, required. Specifies the value to be searched in the array.
  • Parameter array, required. Specifies the array to be searched.
  • Parameter type. optional. If this parameter is set to true, check whether the data to be searched is of the same type as the value of the array.

Returns true if the given value exists in the array. If the third parameter is set to true, the function returns true only when the element exists in the array and the data type is the same as the given value.

If no parameter is found in the array, the function returns false.

If the value parameter is a string and the type parameter is set to true, the search area is case sensitive.

 

Program output:

Match found

Another example program:

 ";}else{  echo "Match not found
";}if (in_array("Glenn",$people, TRUE)){ echo "Match found
";}else{ echo "Match not found
";}if (in_array(23,$people, TRUE)){ echo "Match found
";}else{ echo "Match not found
";}?>

Program output:

Match not foundMatch foundMatch found

When an executable file such as cmdexe is uploaded to the server, we can write a program to determine the type of the file to be uploaded. otherwise, files that do not conform to the type will be rejected. ...

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.