PDA

View Full Version : file -bi ::: mime type???


sev
01-24-2002, 08:05 AM
for a php script i need to find out the mime type of files. while for linux (and probably most other unix systems) the shell command file -bi filename outputs exactly that, the file command for darwin does not know these arguments. the man pages are quite similar except for those arguments.

so my question: is there another way to achieve this? and do you know why darwin behaves differently here to other unix systems?

any help much appreciated! thanks!

sev
01-24-2002, 12:27 PM
already found an answer - marc liyanage, a friend of mine, perl expert, did this:

#!/usr/bin/perl


foreach $line (<STDIN>) {

($type, $mappings) = $line =~ /^(\S+)\s+(\S.+)$/;

next unless ($mappings);

@extensions = split(/\s+/, $mappings);

if (grep {$_ =~ /$ARGV[0]/io} @extensions) {
print $type;
last;
}

}

this will find the appropriate mime type to an extension.

usage: cat $mimeTypesPath | getmime.pl $fileExtension

mervTormel
01-24-2002, 12:40 PM
...why darwin behaves differently here to other unix systems?

looks like apple chose Ian Darwin's version of the file utility, for whatever reason.

at the end of the man page, there's some history and other information.

thanks for the perl magic.

i'm not clear on the usage though.

could you give us a practical example with results?

thanks.

sev
01-24-2002, 03:04 PM
> i'm not clear on the usage though.
> could you give us a practical example with results?

of course, i'll try. i needed this for a quite specific task, but there may be use for other things too. okay, here we go:

i have two local virtual servers:

localhost:10222 -> i call that one papa from now on.
localhost:10333 -> i call that one bubu from now on.

there are three files on papa: papa/eyes.gif, papa/ears.jpg, papa/descr.html
there are two files on bubu: bubu/ears.gif, bubu/descr.html

bubu shall 'inherit' all files from papa. so when i try to access: bubu/eyes.gif, i shall get papa's eyes.gif. i solved this via rewrite rules (see apache's rewrite_mod). an abstract of .htaccess:

RewriteRule ^LOCAL/(.*) - [L]
RewriteRule ^(.+) /LOCAL/fileserver.php\?request=/$1 [L]

so now, when i call http://bubu/eyes.gif, i will actually call http://bubu/LOCAL/fileserver.php?request=/eyes.gif

the fileserver php script will check wether eyes.gif exists on bubu, and if not, it'll go to papa (on same computer) to see if it's there. it'll find it on papa, so it has to deliver it to the browser. to do that, i needs to find out the content-type for the header. and this content-type could be delivered by the file command, though not so on mac os x. thus we take advantage of apache's mime.types config file, and search this via the perl script mentioned. in php, this would look like that:

// now lets find out the extension
$fileExtension = substr( $finallink , strrpos( $finallink , '.' ) );

// ...and now we get the mime type from apache's config file mime.types
$mimeTypesPath = "/private/etc/httpd/mime.types";
$fp = popen( "cat $mimeTypesPath | getmime.pl $fileExtension" , 'r' );
if ( !$fp ) {
echo "ERROR: Das perl-ding geht nicht...";
exit;
} else {
while ( $string = fgets( $fp , 1024 ) ) {
$contentType .= $string;
}
pclose( $fp );
}

// let's send the baby to the browser
header( 'Content-type: '.$contentType );
header( 'Content-Disposition: inline; filename="'.$fileName.'"' );
header( 'Content-length: '.(string)(filesize($finalLink)) );
$fd = fopen( $finalLink , 'r' );
fpassthru( $fd );

hope this makes things a bit clear. :-) if you want all source codes (php, pl, htaccess) just let me know (i thought i try to keep it a bit short). oh, and.. ahem... i'm a flash developer... there might certainly be better ways to achieve this. i'm glad when it works =)

mervTormel
01-24-2002, 03:11 PM
sev,

fantastic! thanks for the comprehensive post.

perhaps in the future we could create a lib for sources of things like this.

-mt