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

Source: Internet
Author: User
Keywords Web Programming PHP Tutorials
Tags array code exit file file name extension file type files function

PHP Tutorial To determine the file type of upload file multiple instance code


$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 (' Your uploaded file type is not allowed ');
Exit;
}

/*
  function Resolution:
  Array Array This does not say
  strtolower characters to lowercase
  substr character interception, the Chinese processing is not friendly.
  Strrpos The position where the character appears in the specified string
  explode split function, return result array
  End read Data last value
  $_files global variable file upload
  getimagesize  Get the type of picture
  In_array Determines if the variable is in an array
  exit terminates the current script run
 
*/
&NBSP

 

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.