PDA

View Full Version : Need help getting finfo_file() script to work.



james438
08-25-2008, 07:18 PM
four questions:

1. what is Fileinfo?
2. how can I tell if I have it installed?
3. can you give an example script so I can test it out?
4. why would you want to use it?

If anyone knows the answer to any one of these I would love to hear it. You don't have to answer all of them in one post if you don't want to.

Nile
08-25-2008, 07:42 PM
Take a look at PHP.nets FileInfo (http://us3.php.net/fileinfo). Here's an intro to fileinfo():
http://us3.php.net/manual/en/intro.fileinfo.php. Take a look at php.net before asking about the function itself.

james438
08-25-2008, 07:49 PM
I did, that is why I am asking :rolleyes:

I always go there first and search via google next before posting here.

Nile
08-25-2008, 07:51 PM
Looking at the intro. It seems like it trys to guess the file type by looking for a certain magic byte sequence. Take a look at the fileinfo() series of functions:
http://us3.php.net/manual/en/ref.fileinfo.php

james438
08-25-2008, 07:53 PM
Did that... I could not get their examples to work and this is an area of php I am quite unfamiliar with. Nothing is listed in the forums here that is of much help either, that is why I am asking for help here...

More specifically I was wanting to know more about the finfo_file function. I did not see anything in the instructions about detecting whether it is already installed. I was not able to find what I was looking for after looking through the entitre (but short) section on fileinfo about how to detect if it is installed. Maybe I was looking in the wrong place?

Nile
08-25-2008, 08:00 PM
You've gotta install it off PECL.

james438
08-25-2008, 08:45 PM
I have already looked at the sites you are trying to direct me to long before you mentioned them, which is why I asked about them in this forum. I try to find the answer first myself before submitting a post.

I just want to know if it is installed not how to install it as I said in my first post and some explanation as to what the people at php.net are talking about when they describe how it works. Some things on php.net are easier to understand than others and this one is of medium difficulty, but the examples I have tried do not seem to be working which is one reason I want to know if it is already installed or not. My best guess is that
<?php
phpinfo();
?> might have the info I am looking for, but I am not sure what it would be listed under and I did use phpinfo() to see if I could find something under file or fileinfo or PCEL, but with no luck. This does not mean that it is not installed.

I have tried
<?php
$finfo = finfo_open(FILEINFO_MIME); // return mime type ala mimetype extension
foreach (glob("*") as $filename) {
echo finfo_file($finfo, $filename) . "\n";
}
finfo_close($finfo);
?> and have tried to adapt it to my own site, but without success. Even if I were to get it to work I am not sure as to the purpose it would serve. From what I have read the last day or two from php.net this function seems to be a buggy function of php and bugs have been filed with php, but I would like to see for myself whether finfo_file() works or not by testing a working example that someone here could post.

So,

1. what is fileinfo? It is a way to guess the content type of a file. Is this the mime type? What does guess mean? what are these magic byte sequences that are mentioned?
2. how can I tell if I have it installed? I do not want to install it, just see if it is installed.
3. can you give an example script so I can test it out? The examples I have tried come up with errors for answers. Am I not adapting the code properly to my site or is it simply not installed.
4. why would you want to use it? Even if it is not installed on my server I am rather curious as to its purpose. It would seem to me that any almost any file editor script would be able to detect this anyway.

I have already looked into these things before posting here, which is the reason I posted here. I could not discover the answer to on my own after visiting php.net or googling for the answer.

jackbenimble4
08-25-2008, 10:24 PM
1. fileinfo is a PHP extension. Yes, the content type is another phrase for mime type. It is impossible to discern the intentions of whoever created the file. All we can do is examine the file and see which mime type it most closely resembles. It could be that the mime type is unknown.

2. Use the phpinfo() function to get a print out of your PHP configuration. Then, search the page for "fileinfo." If there is no module for fileinfo (doesn't matter if there is no data under it), then it's not installed. I'm talking about the ones in <h2> tags. Here's an example on my server. (http://xboxamerica.com/jackson/fileinfoexample.png)

3. It would be more helpful if you supplied the errors rather than ask for new examples. The examples on the php manual are correct, so it is likely the extension is just not installed, or the path to the magic database file is incorrect.

4. It's only purpose is to guess the mime type of a file. I'm not sure what you mean "any file editor script." It is likely that any PHP application that determines mime type uses this extension.

james438
08-26-2008, 04:41 AM
That was quite helpful jackbenimble4 :) It would appear after looking at my phpinfo and not finding any mention of fileinfo installed on my hosted server that fileinfo is not installed. I'm not really interested in stalling anything to my php at this point so all of my questions have been answered.

It is kind of a moot point now, but the code I used was
<?php
$filename="/images/alita3.jpg";
$finfo = finfo_open(FILEINFO_MIME); // return mime type ala mimetype extension
foreach (glob("*") as $filename) {
echo finfo_file($finfo, $filename) . "\n";
}
finfo_close($finfo);
?>
and the error I received was
Fatal error: Call to undefined function: finfo_open() in /home/content/a/n/i/animeviews/html/test.php on line 3
I was under the impression that you could determine the content type of a file simply by looking at the extension, but it appears that this is not the case. When a file editor is used the extensions are displayed, but if finfo_type can determine the contents of the file using a completely different method then using a file editor or other simple folder scanning script is, well, something different.

Again, thanks for the info :)

jackbenimble4
08-26-2008, 07:53 PM
No problem.

The problem with looking at the extension, is that you could take an executable for example (.exe) and rename it as a png image (.png). It could potentially be exploited to somehow run the executable on the server.