Several methods for php to determine the file type to Upload
/**
- Desc: determine the type of the uploaded file
- Link: bbs.it-home.org
- Date:
- */
- $ Array = array ('jpg ', 'GIF', 'PNG', 'jpeg ');
- $ PicImg = '/upfile/upload_pic/thumbnail_1258615556.jpg ';
$ Img = strtolower ($ picImg );
// Method 1 for obtaining the file extension
- $ Ext = substr ($ img, strrpos ($ img, '.') + 1); // code for reading the file extension
// Method 2
- $ Ext = end (explode ('.', $ img ));
// Method 3 for obtaining the file extension. this is probably the safest method, that is, php $ _ FILES ['type'].
- $ Ext = $ _ FILES ['file'] ['type'];
// Method 4 for obtaining the file extension
- $ Ext = getimagesize ($ img); // This function returns an array
If (! In_array ($ ext, $ array ))
- {
- Exit ('thumbnail address is incorrect. please upload it again! ');
- }
- Else
- {
- Echo ('The file type you uploaded is disallowed ');
- Exit;
- }
/*
- Strtolower converts the character into lowercase letters
- Substr character truncation, unfriendly to Chinese processing.
- Strrpos determines the position of a character in a specified string
- Explode split function. the returned result is an array.
- End reads the last value of the data.
- $ _ FILES global variable file Upload
- Getimagesize: obtain the image type
- In_array: determines whether the variable is in the array.
- Exit to terminate the current script
- */
- ?>
|