fileinfo函数PHP

时间:2011-03-22 20:20:48

标签: php fileinfo

有人可以举例说明如何使用fileinfo来替换一段代码,例如:

($_FILES["fileToUpload"]["type"] == "image/gif"
|| $_FILES["fileToUpload"]["type"] == "image/jpeg"
|| $_FILES["fileToUpload"]["type"] == "image/png")

3 个答案:

答案 0 :(得分:6)

使用此:

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

$ fileinfo应该包含正确的MIME类型,您可以在这样的代码段中使用,或者在如下的switch语句中使用:

switch($fileinfo) {
    case "image/gif":
    case "image/jpeg":
    case "image/png":
        // Code
        break;
}

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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