fileinfo apc (solved)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

fileinfo apc (solved)

Post by scarface222 »

Hey guys I just installed file_info extension with great difficulty, and limited documentation to receive the following errors.

Warning: finfo_file(): supplied argument is not a valid file_info resource in /home/collab13/public_html/testing123.php on line 5

Warning: finfo_close(): supplied argument is not a valid file_info resource in /home/collab13/public_html/testing123.php on line 6

Has anyone been through this process before? If so please let me know if you have any idea as to how to get this extension to work, it has givin me great trouble and I am considering switching to the magic mime type but do not want to give up. This is my code, the magic extension seems to be loading since that error has gone away.

Code: Select all

if (isset($_POST['submit'])){
$finfo = finfo_open(FILEINFO_MIME,'/usr/local/apache/conf/magic.mime'); 
var_dump($finfo);// return mime type ala mimetype extension
$mime=finfo_file($finfo, $_FILES['file']['tmp_name']);
finfo_close($finfo);
 
echo $mime;
}
and the form

Code: Select all

<form  action="" method="post" enctype="multipart/form-data" >
 
  <input type="file" name="file" id="file" />
  <input type="submit" name="submit" id="submit" value="Upload!" />
  
  </form>
Last edited by scarface222 on Fri Jan 15, 2010 12:34 pm, edited 4 times in total.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: file_finfo not valid resource, has anyone used this?

Post by Eran »

Did you make sure /usr/local/apache/conf/magic.mime exists and contains the mime information? also, what is the exact printout of the var_dump on $finfo?
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: file_finfo not valid resource, has anyone used this?

Post by scarface222 »

Thanks for the response, when I contacted my system admin he claimed the mime file existed and pointed the php info information as follows:

mime_magic
mime_magic support enabled


mime_magic.magicfile /usr/local/apache/conf/magic

as for the var dump, that was another thing, when I tested it earlier it came up as

bool(false)
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: file_finfo not valid resource, has anyone used this?

Post by Eran »

Since $finfo is false it indicates that it wasn't able to successfully open the magic.mime file. If I remember correctly (and it has been a while), you need to point the function to the directory and not the actual file. In any case, you should play with the location of the mime file you put in the finfo_open() function until it returns a fileinfo resource (and not a false value). Check out the comments on the php.net finfo_open() page -
http://www.php.net/manual/en/function.finfo-open.php
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: file_finfo not valid resource, has anyone used this?

Post by AbraCadaver »

pytrin wrote:Since $finfo is false it indicates that it wasn't able to successfully open the magic.mime file. If I remember correctly (and it has been a while), you need to point the function to the directory and not the actual file. In any case, you should play with the location of the mime file you put in the finfo_open() function until it returns a fileinfo resource (and not a false value).
Yes, and since it is already specified in the php.ini (if it is correct), then there's no need to pass it in the finfo_open() call.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: file_finfo not valid resource, has anyone used this?

Post by Eran »

No, that configuration option is for the mimetype extension. fileinfo is a PECL extension and does not rely on that setting (yet, it's supposed to be integrated into the core PHP source soon).
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: file_finfo not valid resource, has anyone used this?

Post by scarface222 »

Yeah I have been doing that, I will try a bit more but initially, when I left that empty it said cannot load magic and when I put that in the error went away. I looked at the page you sent me, some people said only directory needed, others said extension needed and one guy said there may even be two magic files needed...(I hope not), I will play with it some more but I just don't see why it wouldn't still give me the error cannot load magic. I have messed with the path in almost every way possible...
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: file_finfo not valid resource, has anyone used this?

Post by scarface222 »

ok I tried this code here

Code: Select all

$finfo = new finfo_open(FILEINFO_MIME, "/usr/local/apache/conf/magic"); // return mime type ala mimetype extension
 
if (!$finfo) {
    echo "Opening fileinfo database failed";
    exit();
}
I get these errors

opening fileinfo database failed,

so you are right...how do I be sure if it is the right file I am looking for? also sometimes this error goes away

Failed to load magic database at '/usr/local/apache/conf/magic' depending on how I mod my directory

but yet I always get opening fileinfo database failed which means the function failed, is my syntax right? weird..
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: file_finfo not valid resource, has anyone used this?

Post by Eran »

Did you make sure the file physically exists in that directory?
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: file_finfo not valid resource, has anyone used this?

Post by scarface222 »

no under php info i get

mime_magic.magicfile /usr/local/apache/conf/magic

and

mime_magic support enabled

I am on a virtual private network, I do not think I can access that directory, I will ask my admin to check
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: file_finfo not valid resource, has anyone used this?

Post by AbraCadaver »

pytrin wrote:No, that configuration option is for the mimetype extension. fileinfo is a PECL extension and does not rely on that setting (yet, it's supposed to be integrated into the core PHP source soon).
OK, I've never specified the magic file when I've used it. It must use the system default. If you can't get finfo working, this is what I use:

Code: Select all

$type = exec('file -bi ' . $_FILES['file']['tmp_name']);
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: file_finfo not valid resource, has anyone used this?

Post by scarface222 »

hahahaha it works! I am excited to be making process on this issue. Abra before I get too excited what is this exec('file -bi) part of the equation? Is it using the fileinfo extension which I so painfully am trying to configure or a default php function?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: file_finfo not valid resource, has anyone used this?

Post by AbraCadaver »

scarface222 wrote:hahahaha it works! I am excited to be making process on this issue. Abra before I get too excited what is this exec('file -bi) part of the equation? Is it using the fileinfo extension which I so painfully am trying to configure or a default php function?
No, exec() executes a system command, in this case the linux file command which is awesome. http://linuxmanpages.com/man1/file.1.php
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: file_finfo not valid resource, has anyone used this?

Post by scarface222 »

Is it safe though? How is it compared to fileinfo? If the file is executed to be tested, what if it is malicious code won't my system then be affected? Also, pytrin if you see this what is your opinion, I want as much info as possible before I consider abandoning fileinfo which was a pain in the ass to get this far. Also, if this works which uses a magic file, shouldn't fileinfo?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: file_finfo not valid resource, has anyone used this?

Post by AbraCadaver »

scarface222 wrote:Is it safe though? How is it compared to fileinfo? If the file is executed to be tested, what if it is malicious code won't my system then be affected? Also, pytrin if you see this what is your opinion, I want as much info as possible before I consider abandoning fileinfo which was a pain in the ass to get this far. Also, if this works which uses a magic file, shouldn't fileinfo?
It is safe, but since you're passing user data into it I should have used escapeshellarg():

Code: Select all

$type = exec('file -bi ' . escapeshellarg($_FILES['file']['tmp_name']));
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply