fileinfo function PHP

为君一笑 提交于 2019-12-01 01:27:52

Using this:

$finfo = new finfo();
$fileinfo = $finfo->file($file, FILEINFO_MIME);

$fileinfo should contain the correct MIME type which you would be able to use in a snippet like that, or in a switch statement like:

switch($fileinfo) {
    case "image/gif":
    case "image/jpeg":
    case "image/png":
        // Code
        break;
}
$objInfo = new finfo(FILEINFO_MIME);
list($strImageType, $strCharset) = $objInfo->file($_FILES['fileToUpload']['tmp_name']);
//then use the variable $strImageType in your conditional
if ($strImageType == "image/gif" || $strImageType == "image/jpeg" || $strImageType == "image/png") {
//code
}

Use FILEINFO_MIME_TYPE

$filename = 'example.jpeg';

// Get file info
$finfo = new finfo();
$fileinfo = $finfo->file($filename);                    // return JPEG image data, JFIF standard 1.01, resolution (DPI), density 72x72, segment length 16, progressive, precision 8, 875x350, frames 3
$info = $finfo->file($filename, FILEINFO_MIME);         // return 'image/jpeg; charset=binary'

$type = $finfo->file($filename, FILEINFO_MIME_TYPE);    // return 'image/jpeg'

switch($type)
{
    case 'image/jpg':
    case 'image/jpeg':
        // code...
        break;

    case 'image/gif':
        // code...
        break;

    case 'image/png':
        // code...

    default:
        // error...
        break;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!