开发者

Validate media objects in PHP

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜