how to validate file upload in drupal how to validate file upload in drupal
means,

in my form allowed only txt file, but now user also can upload image file also after change the extention .jpg -> .txt

function oz_workbench_moderation_buttons_form_alter(&amp;$form, $form_state, $form_id) {
  dpm($form);
  if ($form_id == 'catalog_node_form'  )
    {
    $form['#validate'][] = 'upload_validate_function';   
       return $form;
    }
}

function upload_validate_function(&amp;$form, $form_state){
$extensions = array('txt' =&gt; 'text/plain; charset=us-ascii','doc' =&gt; 'application/msword; charset=binary','pdf' =&gt; 'application/pdf; charset=binary','xls' =&gt; 'application/octet-stream; charset=binary','docx' =&gt; 'application/vnd.openxmlformats-officedocument.wordprocessingml.document; charset=binary');
$uploadedfile=count($form_state['values']['field_application_document']['und']);
        for($i=0;$i&lt;$uploadedfile-1;$i++){
                $file_name=$form_state['values']['field_application_document']['und'][$i]['fid']; 
                $file_handler = file_load($file_name);
                $file = $file_handler-&gt;uri;
                $filename = $file_handler-&gt;filename;
                //echo $file;
                $finfo = new finfo(FILEINFO_MIME);
                $type = $finfo-&gt;file($file);
                //echo $type;
                    if (!in_array($type, $extensions) )
                    {
                    drupal_set_message(t($filename.' is not valid file'), 'error');
                    }
         }
}
If your are writing your own validation u can use finfo_file from php

http://www.php.net/manual/en/function.finfo-file.php

Example #1 A finfo_file() example 

&lt;?php
  $finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension
  foreach (glob("*") as $filename) {
    echo finfo_file($finfo, $filename) . "\n";
  }
  finfo_close($finfo);
?&gt;


The above example will output something similar to:


text/html
image/gif
application/vnd.ms-excel