note 91646 added to function.mime-content-type
| From: | Bob at osu1 dot php dot net | Date: | Sun, 21 Jun 2009 11:25:36 +0000 |
| Subject: | note 91646 added to function.mime-content-type | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-157193@lists.php.net to get a copy of this message | ||
I see a lot of comments suggesting doing file extension sniffing (i.e. assuming .jpg files are JPEG
images) when proper file-type sniffing functions are unavailable.
I want to point out that there is a much more accurate way.
If neither mime_content_type() nor Fileinfo is available to you and you are running *any* UNIX
variant since the 70s, including Mac OS, OS X, Linux, etc. (and most web hosting is), just make a
system call to 'file(1)'.
Doing something like this:
<?php
echo system("file -bi '<file path>'");
?>
will output something like "text/html; charset=us-ascii". Some systems won't add the
charset bit, but strip it off just in case.
The '-bi' bit is important. However, you can use a command like this:
<?php
echo system("file -b '<file path>'"); // without the 'i' after
'-b'
?>
to output a human-readable string, like "HTML document text", which can sometimes be
useful.
The only drawback is that your scripts will not work on Windows, but is this such a problem? Just
about all web hosts use a UNIX.
It is a far better way than just examining the file extension.
----
Server IP: 117.104.162.229
Probable Submitter: 58.96.44.86 (proxied: 58.96.44.86)
----
Manual Page -- http://www.php.net/manual/en/function.mime-content-type.php
Edit -- https://master.php.net/note/edit/91646
Del: integrated -- https://master.php.net/note/delete/91646/integrated
Del: useless -- https://master.php.net/note/delete/91646/useless
Del: bad code -- https://master.php.net/note/delete/91646/bad+code
Del: spam -- https://master.php.net/note/delete/91646/spam
Del: non-english -- https://master.php.net/note/delete/91646/non-english
Del: in docs -- https://master.php.net/note/delete/91646/in+docs
Del: other reasons-- https://master.php.net/note/delete/91646
Reject -- https://master.php.net/note/reject/91646
Search -- https://master.php.net/manage/user-notes.php