Home > Backend Development > PHP Tutorial > PHP prohibits certain types of uploaded files_PHP Tutorial

PHP prohibits certain types of uploaded files_PHP Tutorial

WBOY
Release: 2016-07-13 10:33:40
Original
1057 people have browsed it

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;
    }
}
Copy after login

in_array() function

in_array() function searches an array for a given value. Usage: in_array(value,array,type).

  • Parameter value, required. Specifies the value to search for in the array.
  • Parameter array, required. Specifies the array to search.
  • Parameter type, optional. If this parameter is set to true, it is checked whether the type of the searched data and the value of the array are the same.

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";
}
?>
Copy after login

Program output:

Match found
Copy after login

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 />";
}
?>
Copy after login

Program output:

Match not found
Match found
Match found
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/752447.htmlTechArticleIn order to prevent some people from uploading executable files such as exe to the server, we can write a program to determine the uploaded file type, then files that do not conform to the type will be refused to be uploaded. Next...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template