Checking the type of an uploaded file

I am creating a php upload script that uses the "copy" function, ie. coping the file from the temporary folder to a permanent folder. However before I do this I naturally wish to validate its type. In the past I have used "_type" that is set a system variable when you upload, however the location of this script requires a more secure code. As you know "_type" only uses mime types but they just look at the extensions of the file not the file itself.
 
So the question basically is how can I use php or apache or unix code to look at the file header (or deeper into the file) to work out the file type?? My preference is php but I am open to suggestions as this is such a security hazard.

Many thanks for the help.

Richard
RICHARDHAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sjohnstone1234Commented:
There's this:

http://uk.php.net/manual/en/ref.mime-magic.php

but apparently it's not very good (never used it myself but saw a few references to it). And it's deprecated, and it might not be installed on your server.

There's the UNIX 'file' command, which consults the 'magic' database which returns information about all sorts of files, although tends to result in a fairly haphazard human-readable answer which might not be great for use in scripts.

What are you doing with the files once they've been uploaded - storing them for later retrieval by a client, or performing some kind of processing maybe? Might be able to suggest a more reliable method if we can narrow it down to particular file types (e.g. images) and purpose...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.