views:

22

answers:

1

I want to allow users on my website to link to remote media objects: flash, music, movies, etc. I already have a regex to check for links:

/(http|ftp|https):\/\/[\w-]+(.[\w-]+)+([\w-.,@?^=%&:\/~+#]*[\w-\@?^=%&\/~+#])?/i

With the matched links, I want to check if they are media files instead of just checking for the the extension on the URL. I was thinking that I could just check the the HTTP headers for the content-type, but that might be a little too much—I was hoping that PHP might have some native functions already.

A: 

The FileInfo extension provides the finfo_file function, which will give you the mimetype of a file.

From its introduction manual page:

The functions in this module try to guess the content type and encoding of a file by looking for certain magic byte sequences at specific positions within the file. While this is not a bullet proof approach the heuristics used do a very good job.

Daniel Vandersluis
Note: before PHP 5.3.0 you would use `mime_content_type` or had to install fileinfo from PECL
Gordon