Page 2 of 3

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

Posted: Sat Jan 09, 2010 1:17 pm
by AbraCadaver
Also, have you tried just:

Code: Select all

$finfo = finfo_open(FILEINFO_MIME);

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

Posted: Sat Jan 09, 2010 1:36 pm
by scarface222
I tried that I just get then...bloody pain

Warning: finfo_open() [function.finfo-open]: Failed to load magic database at '/usr/share/misc/magic'. in /home/collab13/public_html/testing123.php on line 3

maybe I should just use your way. I guess that is a unix file command that you showed me?

Is it recommendable for file uploads for checking mp3s and images? (i use getimagesize for images but need something good for mp3s) I asked is it safe because I basically want the most accurate way possible to prevent people from uploading malicious files. I read your article and explained a lot how it worked but not so much whether it is accurate enough to use for uploads.

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

Posted: Sat Jan 09, 2010 2:28 pm
by AbraCadaver
scarface222 wrote:I tried that I just get then...bloody pain

Warning: finfo_open() [function.finfo-open]: Failed to load magic database at '/usr/share/misc/magic'. in /home/collab13/public_html/testing123.php on line 3

maybe I should just use your way. I guess that is a unix file command that you showed me?

Is it recommendable for file uploads for checking mp3s and images? (i use getimagesize for images but need something good for mp3s) I asked is it safe because I basically want the most accurate way possible to prevent people from uploading malicious files. I read your article and explained a lot how it worked but not so much whether it is accurate enough to use for uploads.
It is using the same magic file that finfo uses by default so it should be as accurate. Also, if you really want to use the /usr/local/apache/conf/magic file, then you can pass that to 'file' with the -m option.

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

Posted: Sat Jan 09, 2010 2:33 pm
by scarface222
thanks man, the magic file must not work then, I used -mi and got no response, however bi does. I will try to figure this stupid thing out. What bugs me is that under php info it states the directory to the magic file mime_magic.magicfile /usr/local/apache/conf/magic, however there must be something wrong. Thanks for your help everyone I will try to figure out this thing.

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

Posted: Sat Jan 09, 2010 3:24 pm
by AbraCadaver
scarface222 wrote:thanks man, the magic file must not work then, I used -mi and got no response, however bi does. I will try to figure this stupid thing out. What bugs me is that under php info it states the directory to the magic file mime_magic.magicfile /usr/local/apache/conf/magic, however there must be something wrong. Thanks for your help everyone I will try to figure out this thing.
You need to do it like this (file will add the .mime to the end of the magic), but -bi should be sufficient:

Code: Select all

file -bim /usr/local/apache/conf/magic filename

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

Posted: Sat Jan 09, 2010 3:50 pm
by scarface222
Thanks for the reply man I did it like this and still no response, so I am contacting my system admin to get this sorted, the file is stated to exist, but it seems like there might be a problem with it if neither methods work

$type = exec('file -bim /usr/local/apache/conf/magic' . escapeshellarg($_FILES['file']['tmp_name']));

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

Posted: Sat Jan 09, 2010 3:56 pm
by scarface222
I contacted my admin and he said the magic file /usr/local/apache/conf/magic is in the right place with right syntax... this is ridiculous, if no one else has a clue on how to solve this stupid magic file usage problem then I guess I will just use abra's method for now.

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

Posted: Sun Jan 10, 2010 9:57 am
by AbraCadaver
scarface222 wrote:Thanks for the reply man I did it like this and still no response, so I am contacting my system admin to get this sorted, the file is stated to exist, but it seems like there might be a problem with it if neither methods work

$type = exec('file -bim /usr/local/apache/conf/magic' . escapeshellarg($_FILES['file']['tmp_name']));
Try a space after /magic

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

Posted: Sun Jan 10, 2010 2:43 pm
by scarface222
No dice man...I asked the system admin to place the magic file in my php ext directory. The only thing that I can think of is that since I am on a vps network, maybe I don't have access to that file since it is not in my working space.

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

Posted: Sun Jan 10, 2010 3:11 pm
by Eran
Permissions could definitely be a problem. I'd suggest uploading the file yourself to a directory you can manage, and then pointing fileinfo or the exec command to that.

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

Posted: Sun Jan 10, 2010 6:17 pm
by scarface222
I found the file and uploaded to public html folder and set its permissions to 777 just to test. I ran function like so and got this error. I will mess with extension but this is ridiculous haha.

if (isset($_POST['submit'])){
$finfo = finfo_open(FILEINFO_MIME, "magic.mime"); // return mime type ala mimetype extension

if (!$finfo) {
echo "Opening fileinfo database failed";
exit();
}

error
Warning: finfo_open() [function.finfo-open]: Failed to load magic

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

Posted: Sun Jan 10, 2010 6:26 pm
by Eran
The magic library is actually 4 files. You can get them from this package - http://downloads.sourceforge.net/gnuwin ... g_mirror=1 (from the comments on the PECL site). The files are inside /share/files
Put them in a directory on your server (or start with your local machine) and point the script there.

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

Posted: Sun Jan 10, 2010 6:58 pm
by scarface222
HAHAHA MY MAN! I got a result man...we did it! It tested some files, it works! I referenced magic/magic.mgc. That's one problem down, now for the final problem hopefully for a while. php apc, an even bigger nightmare haha, I hate to ask but have you ever tried to use it? I get a constantly unresponsive progress bar, and in my get progress script a divide by zero error on the final line. I know apc is installed correctly and rfc1897 is enabled. If you ever had a similar problem let me know, if not you guys really helped me out I will try to solve this one on my own.

Code: Select all

<?php
header('Expires: Tue, 08 Oct 1991 00:00:00 GMT');
header('Cache-Control: no-cache, must-revalidate');
 
if(isset($_GET['uid'])){
   $status = apc_fetch('upload_' . $_GET['uid']);
   echo round($status['current']/$status['total']*100);
}
?>
Thanks for the help man, really appreciate it, you too abra. You guys really helped me out.

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

Posted: Sun Jan 10, 2010 8:25 pm
by Eran
You should add a check whether $status is valid before dividing by it. Possibly the cache key for it is expired. If apc_fetch is working for you then the problem is probably with your scripts and not the extension (also, you don't show where you set the key, might be useful).

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

Posted: Mon Jan 11, 2010 10:36 am
by scarface222
Once again thank you for your time pytrin you are always quite knowledgeable and all around helpful. With regards to the issue at hand, what do you mean by set the key? That was from my first upload script that I was testing, this is my second and a different script which basically gives me the same problem, an unresponsive bar. I got the bar to jump to 100 when the file was done, but no real time progress. It is self contained on one page, with a validation script on a separate page which just contains php checks like (fileinfo) lol although for testing purposes I removed checks. I didn't think it was an apc installation problem, because the information in php info shows the right information is enabled.

This is the upload script I was trying all in one page.

Code: Select all

<?php
if($_SERVER['REQUEST_METHOD']=='POST') {
  $status = apc_fetch('upload_'.$_POST['APC_UPLOAD_PROGRESS']);
  $status['done']=1;
  echo json_encode($status);
  exit;
} else if(isset($_GET['progress_key'])) {
  $status = apc_fetch('upload_'.$_GET['progress_key']);
  echo json_encode($status);
  exit;
}
?>

Code: Select all

<html>
<head>
<script type="text/javascript" src="yui/yahoo.js"></script>
<script type="text/javascript" src="yui/event.js"></script>
<script type="text/javascript" src="yui/dom.js"></script>
<script type="text/javascript" src="yui/animation.js"></script>
<script type="text/javascript" src="yui/dragdrop.js"></script>
<script type="text/javascript" src="yui/connection.js"></script>
<script type="text/javascript" src="yui/container.js"></script>
<link rel="stylesheet" type="text/css" href="yui/container.css" />
 
<script type="text/javascript">
var fN = function callBack(o) {
  var resp = eval('(' + o.responseText + ')');
  var rate = parseInt(resp['rate']/1024);
  if(resp['cancel_upload']) {
    txt="Cancelled after "+resp['current']+" bytes!"; 
  } else {
    txt=resp['total']+" bytes uploaded!";
  }
  txt += "<br>Upload rate was "+rate+" kbps.";
  document.getElementById('pbar').style.width = '100%';
  document.getElementById('ppct').innerHTML = "100%";
  document.getElementById('ptxt').innerHTML = txt;
  setTimeout("progress_win.hide(); window.location.reload(true);",2000);
}
var callback = { upload:fN }
 
var fP = function callBack(o) {
  var resp = eval('(' + o.responseText + ')');
  if(!resp['done']) { 
    if(resp['total']) {
      var pct = parseInt(100*(resp['current']/resp['total']));
      document.getElementById('pbar').style.width = ''+pct+'%';
      document.getElementById('ppct').innerHTML = " "+pct+"%";
      document.getElementById('ptxt').innerHTML = resp['current']+" of "+resp['total']+" bytes";
    }
    setTimeout("update_progress()",500);
  } else if(resp['cancel_upload']) {
    txt="Cancelled after "+resp['current']+" bytes!"; 
    document.getElementById('ptxt').innerHTML = txt;
    setTimeout("progress_win.hide(); window.location.reload(true);",2000);
  }
}
var progress_callback = { success:fP }
 
function update_progress() {
  progress_key = document.getElementById('progress_key').value;
  YAHOO.util.Connect.asyncRequest('GET','form.php?progress_key='+progress_key, progress_callback);
}
 
var progress_win;
 
function postForm(target,formName) {
  YAHOO.util.Connect.setForm(formName,true);
  YAHOO.util.Connect.asyncRequest('POST',target,callback);
/* Is there some event that triggers on an aborted file upload? */
/*   YAHOO.util.Event.addListener(window, "abort", function () { alert('abort') } ); */
  progress_win = new YAHOO.widget.Panel("progress_win", { width:"420px", fixedcenter:false, underlay:"shadow", close:false, draggable:true, modal:true, effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration:0.3} } );
  progress_win.setHeader("Uploading "+document.getElementById('test_file').value+" ...");
  progress_win.setBody('<div style="height: 1em; width: 400px; border:1px solid #000;"> <div id="pbar" style="background: #99e; height: 98%; width:0%; float:left;">&nbsp;</div> <div id="ppct" style="height: 90%; position: absolute; margin: 1 0 0 185;">0%</div></div><div id="ptxt" style="margin: 3 0 0 5">0 of 0 bytes</div>');
  progress_win.render(document.body);
  update_progress();
}
</script>
</head>
<body>
 <form enctype="multipart/form-data" id="upload_form" action="upload.php" onsubmit="postForm('form.php','upload_form'); return false;" method="POST">
  <input type="hidden" name="APC_UPLOAD_PROGRESS" id="progress_key" value="<?php echo uniqid()?>"/>
  <input type="file" id="test_file" name="test_file"/><br/>
  <input type="submit" value="Upload!"/>
 </form>
 <div id="progress_win"> 
    <div class="hd" style="color: #222; background: #bbb"></div> 
    <div class="bd"></div> 
    <div class="ft"></div> 
 </div> 
</body>
</html>
This was upload.php simple script I used for testing

Code: Select all

<?php
if($_FILES['test_file']['error'] == UPLOAD_ERR_OK){
   $path = 'testingimage/';
   $path .= basename($_FILES['test_file']['name']);
   if(move_uploaded_file($_FILES['test_file']['tmp_name'], $path)){
      // upload successful
   }
}
?>