开发者

fileinfo function PHP

Can someone give me an example of how to use fileinfo, to replace a snippet of code such as:

($_FILES["fileToUpload"]["type"] == "image/gif"
|| $_FILES["fileToUpload"]["type"] == "ima开发者_如何学Cge/jpeg"
|| $_FILES["fileToUpload"]["type"] == "image/png")


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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜