You are not logged in.
- Topics: Active | Unanswered
Pages: 1
#1 2009-02-06 18:38:51
- Franz
- Lead developer

- From: Germany
- Registered: 2008-05-13
- Posts: 3,755
- Website
Determine file type
Does anybody out there know how I can determine the MIME type of a file? The mime_* functions won't work from PHP 5.3.* on, the fileinfo extension is an extension and therefore not always available and the extension doesn't necessary tell me the MIME type. Is there any other option?
If nobody knows one, it would be enough to differentiate between text files, image files and other (binary) files. Any ideas?
Thanks in advance
Last edited by Franz (2009-02-06 18:44:19)
Offline
#2 2009-02-06 18:53:42
- FSX
- Developer

- From: NL
- Registered: 2008-05-09
- Posts: 803
- Website
Re: Determine file type
I always use the extension to identify a file.
function get_ext($filepath)
{
$explode = explode('.', $filepath);
return $explode[count($explode)-1];
}Last edited by FSX (2009-02-06 18:54:09)
Offline
#4 2009-02-06 20:21:19
- FSX
- Developer

- From: NL
- Registered: 2008-05-09
- Posts: 803
- Website
Re: Determine file type
Well for images I use getimagesize(), but that's only for images.
Offline
#5 2009-02-07 01:09:27
- Reines
- Lead developer

- From: Scotland
- Registered: 2008-05-11
- Posts: 3,140
- Website
Re: Determine file type
fileinfo is part of PHP since 5.3.0, hence the removal of mime_content_type, so just check for the existence of 1 and if it doesn't exist then use the other.
Saying that, I don't like fileinfo at all, I think they have gone totally backwards with that :s
Offline
#6 2009-02-07 17:44:42
- Franz
- Lead developer

- From: Germany
- Registered: 2008-05-13
- Posts: 3,755
- Website
Re: Determine file type
mime_content_type is disabled by default, I believe. So that's not safe either. It's possible to determine the file type when uploading files, so why not here?
Is there a way to read the actual file header?
Also: how can I recognize binary files?
Offline
#7 2009-02-10 12:17:11
- Ruckus
- Member
- From: Danville, Illinois
- Registered: 2008-09-17
- Posts: 12
- Website
Re: Determine file type
I use this function. It attempts to use the FileInfo module if its loaded, otherwise it trys the mime_content_type, and as a fall back it uses a shell command. Because of what I'm using the function for, it takes some special cases into account and returns specific mime types if the extensions match.
public function getMimeType($file){
$type = 'application/octet-stream';
if(function_exists('finfo_open')){ // Pecl
$finfo = finfo_open(FILEINFO_MIME);
$type = finfo_file($finfo, $file);
finfo_close($finfo);
}elseif(function_exists('mime_content_type')){ //attempt deprecated mime_content_type
$type = mime_content_type($file);
}else{ //attempt to use a shell command
$type = shell_exec('file -i -b '.escapeshellarg($file));
$type = explode(' ', $type);
if(is_array($type))
$type = $type[0];
}
if(substr($type, 0, 5) == 'text/'){
$ext = strtolower(substr($file, strrpos($file, '.')));
if($ext == '.php'){
$type = 'application/x-httpd-php';
}elseif($ext == '.phps'){
$type = 'application/x-httpd-php-source';
}elseif($ext == '.css'){
$type = 'text/css';
}elseif($ext == '.js'){
$type = 'text/javascript';
}
}
return $type;
}Offline
Pages: 1
