php get upload file type get file suffix

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags file file type files filetype function get image image file

php tutorial Get upload file type Get file suffix This tutorial provides three ways to get upload files and image types, the method is very simple The first two types is defined first, and then in_array judgment, the last step is to use fopen read the first two bytes, determine.
* /

// Get the image format, including jpg, png, gif
function get_type ($ img_name) // Get the image file type
{
if (preg_match ("/. (jpg | jpeg | gif | png | bmp) $ / i", $ img_name, $ matches)) {
$ type = strtolower ($ matches [1]);
} else {
$ type = "string";
}
return $ type;
}

/ / Determine the type of file upload
$ allowedextensions = array ("txt", "csv", "htm", "html", "xml",
"css tutorial", "doc", "xls", "rtf", "ppt", "pdf", "swf", "flv", "avi"
"wmv", "mov", "jpg", "jpeg", "gif", "png");
foreach ($ _files as $ file) {
if ($ file ['tmp_name']> '') {
if (! in_array (end (explode (".",
strtolower ($ file ['name']))),
$ allowedextensions)) {
die ($ file ['name']. 'is an invalid file type!'
'<a href="#############
'& lt; & lt go back back </a>');
}
}
}

// Another kind of reading file and image type

function checktitle ($ filename) {
$ file = fopen ($ filename, "rb");
$ bin = fread ($ file, 2); // read only 2 bytes
fclose ($ file);
$ strinfo = @unpack ("c2chars", $ bin);
$ typecode = intval ($ strinfo ['chars1']. $ strinfo ['chars2']);
$ filetype = "";
switch ($ typecode)
{
case 7790:
$ filetype = 'exe';
break;
case 7784:
$ filetype = 'midi';
break;
case 8297:
$ filetype = 'rar';
break;
case 255216:
$ filetype = 'jpg';
break;
case 7173:
$ filetype = 'gif';
break;
case 6677:
$ filetype = 'bmp';
break;
case 13780:
$ filetype = 'png';
break;
default:
$ filetype = 'unknown'. $ typecode;
}
// fix
if ($ strinfo ['chars1'] == '- 1' && $ strinfo ['chars2'] == '- 40') {
return 'jpg';
}
if ($ strinfo ['chars1'] == '- 119' && $ strinfo ['chars2'] == '80') {
return 'png';
}
return $ filetype;
}


?>

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.