• PHP Video Tutorials

PHP - FileInfo open() Function



The finfo_open() function can create a new fileinfo resource.

Syntax

resource finfo_open ([ int $options [, string $arg ]] )

Object oriented style (constructor):
finfo
__construct ([ int $options [, string $magic_file ]] )

This function can open a magic database and returns its resource and return a magic database resource on success, or false on failure.

Example - Object oriented style

<?php
   $finfo = new finfo(FILEINFO_MIME, "/usr/share/misc/magic");  
   // return mime type ala mimetype extension

   if(!$finfo) {
      echo "Opening fileinfo database failed";
      exit();
   }
   /* get mime-type for a specific file */
   $filename = "/usr/local/something.txt";
   echo $finfo->file($filename);

   /* close connection */
   $finfo->close();
?>

Example - Procedural style

<?php
   $finfo = finfo_open(FILEINFO_MIME, "/usr/share/misc/magic");  
   // return mime type ala mimetype extension
   
   if(!$finfo) {
      echo "Opening fileinfo database failed";
      exit();
   }
   
   /* get mime-type for a specific file */
   $filename = "/usr/local/something.txt";
   echo finfo_file($finfo, $filename);

   /* close connection */
   finfo_close($finfo);
?>
php_function_reference.htm
Advertisements