fileinfo function PHP

早过忘川 提交于 2019-12-19 04:53:25

问题


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"] == "image/jpeg"
|| $_FILES["fileToUpload"]["type"] == "image/png")

回答1:


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



回答2:


$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
}



回答3:


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


来源:https://stackoverflow.com/questions/5397350/fileinfo-function-php

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