Powerful php Check file type _php tutorial

Source: Internet
Author: User
A powerful file type detection function, it can determine what type of file you upload, this you have no way to install the past, the need for friends can refer to.
The code is as follows Copy Code
**
*
*
* @access Public
* @param string filename
* @param string limit_ext_types allowed file types, with | enclosing types such as: |gif|txt|
* @return String
*/
function Check_file_type ($filename, $limit _ext_types = ") {
$extname = Strtolower (substr ($filename, Strrpos ($filename, '. ') + 1));
if ($limit _ext_types &&
Stristr ($limit _ext_types, ' | '. $extname. '|') = = = False) {
Return ';
}

$str = $format = ";

$file = @fopen ($filename, ' RB ');
if ($file) {
$str = @fread ($file, 0x400); Read the first 1024 bytes
@fclose ($file);
}
else{
$format = $extname;
}

if ($format = = "&& strlen ($STR) >= 2) {
if (substr ($str, 0, 4) = = ' MThd ' && $extname! = ' txt ') {
$format = ' mid ';
}
ElseIf (substr ($str, 0, 4) = = ' RIFF ' && $extname = = ' wav ') {
$format = ' wav ';
}
ElseIf (substr ($str, 0, 3) = = "Xffxd8xff") {
$format = ' jpg ';
}
ElseIf (substr ($str, 0, 4) = = ' GIF8 ' && $extname! = ' txt ') {
$format = ' gif ';
}
ElseIf (substr ($str, 0, 8) = = "x89x50x4ex47x0dx0ax1ax0a") {
$format = ' png ';
}
ElseIf (substr ($str, 0, 2) = = ' BM ' && $extname! = ' txt ') {
$format = ' bmp ';
}
ElseIf ((substr ($str, 0, 3) = = ' CWS ' | | substr ($STR, 0, 3) = = ' FWS ')
&& $extname! = ' txt ') {
$format = ' swf ';
}
ElseIf (substr ($str, 0, 4) = = "Xd0xcfx11xe0") {//d0cf11e = = Docfile = Microsoft Office Document
if (substr ($str, 0x200,4) = = "Xecxa5xc1x00"
|| $extname = = ' Doc ') {
$format = ' Doc ';
}
ElseIf (substr ($str, 0x200,2) = = "X09x08" | | $extname = = ' xls ') {
$format = ' xls ';
}
ElseIf (substr ($str, 0x200,4) = = "Xfdxffxffxff"
|| $extname = = ' ppt ') {
$format = ' ppt ';
}
}
ElseIf (substr ($str, 0, 4) = = "pkx03x04") {
$format = ' zip ';
}
ElseIf (substr ($str, 0, 4) = = ' rar! ' && $extname! = ' txt ') {
$format = ' rar ';
}
ElseIf (substr ($str, 0, 4) = = "X25pdf") {
$format = ' pdf ';
}
ElseIf (substr ($str, 0, 3) = = "x30x82x0a") {
$format = ' cert ';
}
ElseIf (substr ($str, 0, 4) = = ' ITSF ' && $extname! = ' txt ') {
$format = ' chm ';
}
ElseIf (substr ($str, 0, 4) = = "X2ERMF") {
$format = ' RM ';
}
ElseIf ($extname = = ' sql ') {
$format = ' sql ';
}
ElseIf ($extname = = ' txt ') {
$format = ' txt ';
}
}

if ($limit _ext_types &&
Stristr ($limit _ext_types, ' | '. $format. '|') = = = False) {
$format = ";
}

return $format;
}

http://www.bkjia.com/PHPjc/632260.html www.bkjia.com true http://www.bkjia.com/PHPjc/632260.html techarticle a powerful file type detection function, it can determine what type of file you upload, this you have no way to install the past, the need for friends can refer to. The code is as follows ...

  • 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.