Home >Backend Development >PHP Tutorial >PHP prohibits certain types of uploaded files_PHP Tutorial
In order 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 uploaded file, and then files that do not meet the type will be refused to upload.
The following is the PHP program that implements this function:
function ($file_name, $pass_type = array('jpg','jpeg','gif','bmp','png') ) { // 允许文件类型的后缀组成的数组 $file = $pass_type; // 截取上传文件的文件名的后缀 $kzm = substr(strrchr($file_name,"."),1); // 判断此后缀是否在数组中 $is_img = in_array(strtolower($kzm),$file); if($is_img) { return true; } else { return false; } }
in_array() function searches an array for a given value. Usage: in_array(value,array,type).
Returns true if the given value value exists in the array array. If the third parameter is set to true, the function returns true only if the element exists in the array and has the same data type as the given value.
If the parameter is not 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 is case-sensitive.
<?php $people = array("Peter", "Joe", "Glenn", "Cleveland"); if (in_array("Glenn",$people)) { echo "Match found"; } else { echo "Match not found"; } ?>
Program output:
Match found
Another sample program:
<?php $people = array("Peter", "Joe", "Glenn", "Cleveland", 23); if (in_array("23",$people, TRUE)) { echo "Match found<br />"; } else { echo "Match not found<br />"; } if (in_array("Glenn",$people, TRUE)) { echo "Match found<br />"; } else { echo "Match not found<br />"; } if (in_array(23,$people, TRUE)) { echo "Match found<br />"; } else { echo "Match not found<br />"; } ?>
Program output:
Match not found Match found Match found