[thelist] PHP File Uploading, Macintosh, and Mime Types

lists at electricstate.com lists at electricstate.com
Tue Dec 10 11:59:01 CST 2002

All right...kind of an involved problem, but here goes...

I'm attempting to create a small-scale asset management system using PHP and
MySQL. In this system, there are several ways for adding files, including
<input file ...>, as well as grabbing a remote file.

Among other information, I'm storing these files' mime types in a mysql
database. Initially, I was grabbing the mime types from uploaded files from the
$_FILES array ($_FILES[...]['type']), but I realized this wasn't going to work
from files added in other ways. So instead I created a general function,
return_mime_type($path), that uses the UNIX command "file -ib $path" to return
the mime type of a local file. I figured this was the best way - a standard
method of returning mime types on all files added into the asset management

However, there is a problem, and I suspect it's because I'm using a Mac (OS
9.2.2). When uploading/grabbing remote images, everything works fine. I get
"image/gif" and "image/jpeg", etc... However, when uploading MP3 files, "file
-ib" simply returns "data", which is the default to return when the command
can't figure out what type of file it is fed. This happens when I use <input
file ...> as well as when I use an FTP program (Transmit), and then add the
file. However, when I grab an mp3 from a site I've worked on (where users are
adding files using Windows FTP programs), "file -ib" returns "audio/mpeg."
Additionally, when I grab the $_FILES[...]['type'] value from an uploaded MP3
file, it returns "audio/mpeg" - even on the Mac (as it should.)

So basically, in a nutshell: uploading through IE 5.1 Mac (classic) and FTPing
through Transmit creates a valid MP3 (plays fine over the web), but a UNIX
command returns a generic mime type. Grabbing the mime type from the $_FILES
variable returns a valid type. Using the same UNIX command on files uploaded
through other FTP programs returns a valid type.

Anyone have any advice, either good or bad? I wonder if I'm going to have strip
out a resource fork, or something like that.

Thanks to any and all who reply. Oh, and since in the past I've taken so much
from this forum :-) here's a little tip I've found quite useful (actually when
working with the system described above)...

<tip type="Online Asset Management" author="Andrew Embler">
Do you work with images in your web application? Do you use PHP, but the hate
the GD library? Well, grouse no more - ImageMagick <http://www.imagemagick.org>
to the rescue. You don't even need special PHP functions to access it; simply
install the binary (or use an ISP that already has it installed), and access it
from the command line. With one quick command you can convert images between
many different formats, change color palettes, resize images to a specific size
(or as close as possible while maintening the aspect ratio.) Get rid of the
crummy thumbnailing that GD produces.

More information about the thelist mailing list