Home | MySQL Russian Manual | MySQL Manual | Apache HTTP Server Rus Documentation | Apache HTTP Server Documentation | downloads | faq

search for in the  Language: Russian


Fileinfo Functions

Table of Contents


User Contributed Notes
Fileinfo Functions
nessi at nessi dot ch
01-Jan-2010 12:26
For opensuse, you will just need to install the file-devel to solve the Problem with
checking for magic files in default path... not found
configure: error: Please reinstall the libmagic distribution

zypper install file-devel
aidan at php dot net
20-Dec-2008 09:09
As of PHP 5.3, Fileinfo is shipped with the main distribution and enabled by default. The extension is no longer maintained in PECL.
abdo
18-Oct-2008 09:32
Add extension=fileinfo.so in your php.ini file

or (extension=/path/to/fileinfo.so)
Philip Snyder
05-Sep-2008 10:40
This module needs some serious work. I'm a well versed linux guy and it took me almost an hour of hard research & trying this and that before I could actually get this to work. mime_content_type() is supposed to be deprecated, but if this is the replacement, stick with mime_content_type() as long as you can!

After reading the previously mentioned installation issues, I still had to have this modification:

$ apt-get install file-devel
$ wget http://pecl.php.net/get/Fileinfo-1.0.4.tgz
$ tar -zxvf Fileinfo-1.0.4.tgz
$ cd Fileinfo-1.0.4
$ pecl install fileinfo

Note that there is no ./configure anymore -- and that the directory name changed. Trying to do the pecl command first ended in failure.

On my Fedora 7 box, I also had to directly reference the magic db as follows before this would work:

$finfo = finfo_open(FILEINFO_MIME, '/usr/share/file/magic');
$mimeData = finfo_file($finfo, $file);
echo "<pre>mimeData: ".print_r($mimeData, true)."</pre>";

Without it, it tries to use a linked file (/usr/share/misc/magic) -- which just doesn't work for some reason. I traced all the links, and they're right. Such basic functionality should not be so hard to come by!
Evermorian
27-Aug-2008 12:03
In response to the suggestion from "jon at cybus" below to symlink /usr/share/file/magic to /etc/magic.mime, note that this causes other problems (in Debian Etch, anyway).  It breaks the -i functionality of the file command, causing it to return the human-readable string instead of the MIME type.  It also results in finfo doing the same.

So, it is probably better to actually specify the path to the magic file correctly when instantiating your finfo object:

<?php
$fi
= new finfo(FILEINFO_MIME,'/usr/share/file/magic');
$mime_type = $fi->buffer(file_get_contents($file));
?>

Of course, you still end up with something that cannot tell the difference between a Word document and an Excel spreadsheet.
Terren Suydam
21-Jun-2008 06:00
If finfo_file() returns a mime type that also includes a character set definition (separated by a semi-colon), like:

text/plain; charset=us-ascii

Then you'll probably want to leave the charset definition in with the mime type, particularly if you're using the resulting string in an HTTP Content-Length header. The HTTP standard specifically allows for this, see:

http://www.w3.org/International/O-HTTP-charset

It seemed as if some of the previous commenters were trying to remove the charset.
john2496 at gmail dot com
10-Jun-2008 12:00
As of now, mime_content_type, has been depreciated. This version of mime_content_type works well as a replacement and uses a method of mime detection previous posted. Other methods can be added in case the default method does not suffice.

if (!function_exists('mime_content_type'))
{
    function mime_content_type($file, $method = 0)
    {
        if ($method == 0)
        {
            ob_start();
            system('/usr/bin/file -i -b ' . realpath($file));
            $type = ob_get_clean();

            $parts = explode(';', $type);

            return trim($parts[0]);
        }
        else if ($method == 1)
        {
            // another method here
        }
    }
}

// image/png
echo mime_content_type('somepng.png');
joe at bobbriggs dot com
20-Feb-2008 08:35
[Quote]
I had better results using the PEAR 'MIME_Type' package. It gave proper answers for 1 & 3 and identified the PHP file as 'text/plain' which is probably better than a false match for C++
[/Quote]

The irony here is that the 'MIME_Type' package uses mime_content_type() which, according to it's doc page, has been deprecated in favor of fileinfo.

Go figure.

thnx,
Christoph
tom at bitworks dot de
24-Nov-2007 04:35
It sometimes seams to be already helpful, simply to ask if the file contains the character NUL (ord == 0).

No normal textfile will contain an ASCII NUL, thus containing one or even perhaps more of them, it might be an executable or a picture file.

To identify picture types files php provides the function getimagesize()
Paul
08-Nov-2007 04:12
The results of this function seem to be of dubious quality.

eg
1)  a Word doc returns:
'application/msword application/msword'
...ok not too bad, but why does it come back twice?

2)  a PHP file comes back as:
'text/x-c++; charset=us-ascii'
My test file started with '<?php' so not ambiguous really. And where does it get the charset assumption from?

3)  a text doc that starts with the letters '
GIF' comes back as:
'
image/gif'
(just like in DanielWalker'
s example for the unix 'file' command)

I had better results using the PEAR 'MIME_Type' package. It gave proper answers for 1 & 3 and identified the PHP file as 'text/plain' which is probably better than a false match for C++

Both finfo_file and MIME_Type correctly identified my other two test files which were a windows exe renamed with .doc extension, and a PDF also renamed with .doc extension.
DanielWalker at ncfe dot org dot uk
30-Oct-2007 04:32
Using the Unix 'file' program with the -i switch will not work reliably.

Consider the following plain-text CSV file (we'll call it 'error.csv'), which has the contents:

GIFT,WRAPPED,ERRORS,ON,LOOKING,At,FILES

...just that one line: nothing above it, nothing below.

Now, what does asking:

file -i error.csv

...give us for this file? Yes, that's right -

'error.csv: image/gif'

Why? Because the first line starts 'GIF...', that's why. You cannot trust file.
bob at bradley dot com
07-Oct-2007 08:56
This is the most impossible module to get functioning.  There's about zero way to get it to function on OS X, and it's supposed to be the de facto way to pull mime types and other file info?

Seriously, the whole thing and all dependencies need to be bundled with PHP.
jon at cybus dot co dot uk
05-Sep-2007 02:58
To get v1.0.4 working on my Ubuntu Feisty system, I had to do the following. It's probably the same on Debian.

* apt-get install libmagic1-dev
* pecl install Fileinfo
* Add "extension=fileinfo.so" to php.ini (/etc/php5/{cli,cgi}/php.ini)
* ln -s /usr/share/file/magic /etc/magic.mime
hari_1983 at yahoo dot com
23-Aug-2007 05:45
Actually for RPM users file-devel contains whatever libmagic contains. then apart from pear, u can directly use "pecl install Fileinfo".
bujuzu A~T gmail.com
15-Jun-2007 05:22
If, like I was, you are looking for a quick and easy way to grab a mime type on a file, and don't feel like fiddling around with extentions or pear or any of that, a unix native 'file -i' command will usually do the trick - that is, if you are able to use shell commands.
mwwaygoo AT hotmail DOT com
07-Jun-2007 01:25
Be careful of using   system("file -i -b file.pdf");

I got some strange results as follows:-

1.
<?php
$type
=system("file -i -b mime.php");
echo
$type;
?>

text/plain; charset=us-ascii
text/plain; charset=us-ascii

2.
<?php
$type
=system("file -i -b mime.php");
echo
$type."<br/>\n";
$split=split(";",$type);
print_r($split);
$type=trim($split[0]);
echo
$type;
?>

text/plain; charset=us-ascii
text/plain; charset=us-ascii<br/>
Array
(
    [0] => text/plain
    [1] =>  charset=us-ascii
)
text/plain

3.
<?php
$type
=@system("file -i -b mime.php");
//echo $type."<br/>\n";   // just comment this line
$split=split(";",$type);
print_r($split);
$type=trim($split[0]);
echo
$type;
?>

text/x-c++; charset=us-ascii
Array
(
    [0] => text/x-c++
    [1] =>  charset=us-ascii
)
text/x-c++
aidan at php dot net
15-Mar-2007 08:54
PHP Warning:  finfo::finfo(): Failed to load magic database at '/etc/magic'
PHP Warning:  finfo::file(): The invalid fileinfo object

These errors can be rectified by copying your magic database (depending on your distro, this file can be anywhere, on debian it's in /usr/share/file/magic) to /etc/magic.mime

libmagic automatically appends the .mime to the end of the filename, so PHP incorrectly reports the path it was looking for.

The same applies for:
PHP Warning:  finfo::finfo(): Failed to load magic database at '/etc/magic.mime'

Unfortunately users will have to call the magic file /etc/magic.mime.mime in this case.
Alexey
18-Feb-2007 02:39
Well, it is hard to install and use this extension. There is better alternative - use lunux comand "file". For  insturctions - "man file" from linux shell.

<?
echo system("file -i -b file.pdf");
?>

application/pdf
szotsaki at gmail dot com
01-Feb-2007 07:12
I am about to write how installed this package.

First of all, I tried with "pear install fileinfo" - as the manual says.
But the pear command said that 'Package "Fileinfo" is not valid,
install failed'.

Then the "pear install pecl/fileinfo" was a better way. But at that time the "phpize" command was missing.
I installed that (on openSUSE distributions it is in the php5-devel, but I think you can find it in your distro's corresponding php-devel package).

After that you may install "re2c" (I did). It's homepage is: http://sourceforge.net/projects/re2c

Copy the magic file of Apache (usually in /etc/apache2) into the following directory: /usr/locale/share/file/ or /usr/share/file/

Then you have to install "libmagic-dev". If you have Debian based system you can simply install it with apt.
But if you have an rpm based system (like me), you have to download the following package: http://packages.debian.org/unstable/libdevel/libmagic-dev
It contains the files we need.
So, download the file, browse it with Midnight Commander (mc) (you have to apt and dpkg be installed) and simply extract (so copy) the /usr folder (it is inside the CONTENTS folder) of the .deb package to the root folder.

And now give the "pear install pecl/fileinfo" command another try :)

Ps: Don't forget to check whether the script has wrote the following line into the php.ini (on openSUSE: /etc/php5/apache2): extension=fileinfo.so

I hope, I could help.
motin at demomusic dot nu
10-Jan-2007 01:34
I had a real headache trying to install this package through pear/pecl. Ran into what looks like this bug: http://pecl.php.net/bugs/bug.php?id=7673 (phpize fails)

I found downloading the package manually and running ./configure helped show what the problem is:

...
checking for fileinfo support... yes, shared
checking for magic files in default path... not found
configure: error: Please reinstall the libmagic distribution
<quit>

I though this was because of a missing magic-database like magic.mime but examining the configure-script, magic.h is searched for.

Problem for me was that include/magic.h was not found. After some googling about where to find magic.h led me to the dead
simple solution:

apt-get install libmagic-dev

This does NOT solve the original installation bug strangely enough, but allows for manual installation:

1. Find the url to the latest version of fileinfo from http://pecl.php.net/package/Fileinfo (atm: http://pecl.php.net/get/Fileinfo-1.0.4.tgz)

2. Download, compile and install
wget http://pecl.php.net/get/Fileinfo-1.0.4.tgz
gunzip Fileinfo-1.0.4.tgz
tar -xvf Fileinfo-1.0.4.tar
cd fileinfo-1.0.4
./configure
make
make install

3. Add extension=fileinfo.so in your php.ini file

4. Restart Apache
jausions at php dot net
08-Jul-2006 07:26
For Windows users:

1. Go to http://pecl4win.php.net/ to get the php_fileinfo.dll if your PHP installation didn't come with it, and you haven't installed the Extensions package.

2. Then make sure you have extension=php_fileinfo.dll somewhere in your php.ini

3. Restart your web server.
 

 
credits | contact