Detailed Explanation of Example of php Judgment File Upload Picture Format
- 2021-08-10 07:09:20
- OfStack
Detailed Explanation of Example of php Judgment File Upload Picture Format
Judge the file picture type,
$type = $_FILES['image']['tmp_name'];// Filename
//$type = $this->getImagetype( $type );
$filetype = ['jpg', 'jpeg', 'gif', 'bmp', 'png'];
if (! in_array($type, $filetype))
{
return " Is not a picture type ";
}
As above, if the user modifies the file suffix png jpeg, etc., the solution to check the data is to use the binary flow information of the judgment file. If you happen to encounter this problem, try 1:
//* Judge whether the upload format of the picture is a picture or not return Returns the file suffix
public function getImagetype($filename)
{
$file = fopen($filename, 'rb');
$bin = fread($file, 2); // Read-only 2 Byte
fclose($file);
$strInfo = @unpack('C2chars', $bin);
$typeCode = intval($strInfo['chars1'].$strInfo['chars2']);
// dd($typeCode);
$fileType = '';
switch ($typeCode) {
case 255216:
$fileType = 'jpg';
break;
case 7173:
$fileType = 'gif';
break;
case 6677:
$fileType = 'bmp';
break;
case 13780:
$fileType = 'png';
break;
default:
$fileType = ' You can only upload picture type formats ';
}
// if ($strInfo['chars1']=='-1' AND $strInfo['chars2']=='-40' ) return 'jpg';
// if ($strInfo['chars1']=='-119' AND $strInfo['chars2']=='80' ) return 'png';
return $fileType;
}
If you have any questions, please leave a message or go to this site community to exchange and discuss, thank you for reading, hope to help everyone, thank you for your support to this site!