PHP to determine the file type of upload file types of multiple instance code

Source: Internet
Author: User
Tags explode file upload


$array = array (' jpg ', ' gif ', ' PNG ', ' jpeg ');

$PICIMG = '/upfile/upload_pic/thumbnail_1258615556.jpg ';

$img = Strtolower ($PICIMG);

Get file name extension method one

$ext = substr ($img, Strrpos ($img, '. ') +1)//Here is the code to read the file name extension

Get file name extension method two

$ext = End (Explode ('. ', $img));

Get file name extension method Three this is supposed to be the safest, is to use PHP $_files[' type '

$ext = $_files[' file ' [' type '];

Get file name extension method four

$ext = getimagesize ($img);//This function returns an array

if (!in_array ($ext, $array))
{
Exit (' Thumbnail address error, please upload again! ');
}
Else
{
Echo (' The type of file you uploaded is not allowed ');
Exit
}

/*
Function parsing:
Array arrays this doesn't say.
Strtolower convert characters to lowercase
substr character interception, not friendly to Chinese processing.
Strrpos the position in the specified string where the character of the judge appears
Explode Split function, returns the result as an array
End reads the last value of the data
$_files global variable File upload
GetImageSize get the type of picture
In_array determine if the variable is in the array
Exit terminates current script run

*/

/*
Fdsfds

This site original article reproduced annotated from Www.111cn.net
*/
?>

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.