Warm tip: This article is reproduced from stackoverflow.com, please click
php

How do I detect MIME type via a file handle in PHP?

发布于 2020-03-27 15:45:55

I'm on 5.4 and looking to detect a mime type from my file handle. I know I can save off a file and then use functions by passing strings, but we want to avoid using strings. So is there a way without any strings?

Questioner
Dave Stein
Viewed
30
Bryan Latten 2013-08-16 05:12

Instead of passing a file handle or a string, pass an SplFileObject. Using this, you get OO access to the file without directly calling file system functions. Functions that require a pathname can still by used by calling ->getRealPath() on the object.

$finfo     = new finfo(FILEINFO_MIME_TYPE); 
$mime_type = $finfo->file( $fileObject->getRealPath() );