<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Create a new fileinfo resource</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.finfo-file.html">finfo_file</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.finfo-set-flags.html">finfo_set_flags</a></div> <div class="up"><a href="ref.fileinfo.html">Fileinfo Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.finfo-open" class="refentry"> <div class="refnamediv"> <h1 class="refname">finfo_open</h1> <h1 class="refname">finfo::__construct</h1> <p class="verinfo">(PHP >= 5.3.0, PECL fileinfo >= 0.1.0)</p><p class="refpurpose"><span class="refname">finfo_open</span> -- <span class="refname">finfo::__construct</span> — <span class="dc-title">Create a new fileinfo resource</span></p> </div> <div class="refsect1 description" id="refsect1-function.finfo-open-description"> <h3 class="title">Description</h3> <p class="para">Procedural style</p> <div class="methodsynopsis dc-description"> <span class="type">resource</span> <span class="methodname"><strong>finfo_open</strong></span> ([ <span class="methodparam"><span class="type">int</span> <code class="parameter">$options</code><span class="initializer"> = FILEINFO_NONE</span></span> [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$magic_file</code><span class="initializer"> = <strong><code>NULL</code></strong></span></span> ]] )</div> <p class="para rdfs-comment">Object oriented style (constructor):</p> <div class="constructorsynopsis dc-description"> <span class="modifier">public</span> <span class="methodname"><strong>finfo::__construct()</strong></span> ([ <span class="methodparam"><span class="type">int</span> <code class="parameter">$options</code><span class="initializer"> = FILEINFO_NONE</span></span> [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$magic_file</code><span class="initializer"> = <strong><code>NULL</code></strong></span></span> ]] )</div> <p class="para rdfs-comment"> This function opens a magic database and returns its resource. </p> </div> <div class="refsect1 parameters" id="refsect1-function.finfo-open-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">options</code></em></span> <dd> <p class="para"> One or disjunction of more <a href="fileinfo.constants.html" class="link">Fileinfo constants</a>. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">magic_file</code></em></span> <dd> <p class="para"> Name of a magic database file, usually something like <var class="filename">/path/to/magic.mime</var>. If not specified, the <em>MAGIC</em> environment variable is used. If the environment variable isn't set, then PHP's bundled magic database will be used. </p> <p class="para"> Passing <strong><code>NULL</code></strong> or an empty string will be equivalent to the default value. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.finfo-open-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> (Procedural style only) Returns a magic database resource on success or <strong><code>FALSE</code></strong> on failure. </p> </div> <div class="refsect1 notes" id="refsect1-function.finfo-open-notes"> <h3 class="title">Notes</h3> <div class="warning"><strong class="warning">Warning</strong> <p class="para"> The expected magic database format changed in PHP 5.3.11 and 5.4.1. Due to this, the internal magic database was upgraded. This mostly effects code where an external magic database is used: reading an older magic file will now fail. Also, some textual representations of the mime types has changed, for instance for PHP would be "PHP script, ASCII text" instead of "PHP script text" returned. </p> </div> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> Generally, using the bundled magic database (by leaving <em><code class="parameter">magic_file</code></em> and the <em>MAGIC</em> environment variables unset) is the best course of action unless you specifically need a custom magic database. </p> </p></blockquote> </div> <div class="refsect1 examples" id="refsect1-function.finfo-open-examples"> <h3 class="title">Examples</h3> <div class="example" id="example-2345"> <p><strong>Example #1 Object oriented style</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$finfo </span><span style="color: #007700">= new </span><span style="color: #0000BB">finfo</span><span style="color: #007700">(</span><span style="color: #0000BB">FILEINFO_MIME</span><span style="color: #007700">, </span><span style="color: #DD0000">"/usr/share/misc/magic"</span><span style="color: #007700">); </span><span style="color: #FF8000">// return mime type ala mimetype extension<br /><br />/* get mime-type for a specific file */<br /></span><span style="color: #0000BB">$filename </span><span style="color: #007700">= </span><span style="color: #DD0000">"/usr/local/something.txt"</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">$finfo</span><span style="color: #007700">-></span><span style="color: #0000BB">file</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> <div class="example" id="example-2346"> <p><strong>Example #2 Procedural style</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$finfo </span><span style="color: #007700">= </span><span style="color: #0000BB">finfo_open</span><span style="color: #007700">(</span><span style="color: #0000BB">FILEINFO_MIME</span><span style="color: #007700">, </span><span style="color: #DD0000">"/usr/share/misc/magic"</span><span style="color: #007700">); </span><span style="color: #FF8000">// return mime type ala mimetype extension<br /><br /></span><span style="color: #007700">if (!</span><span style="color: #0000BB">$finfo</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"Opening fileinfo database failed"</span><span style="color: #007700">;<br /> exit();<br />}<br /><br /></span><span style="color: #FF8000">/* get mime-type for a specific file */<br /></span><span style="color: #0000BB">$filename </span><span style="color: #007700">= </span><span style="color: #DD0000">"/usr/local/something.txt"</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">finfo_file</span><span style="color: #007700">(</span><span style="color: #0000BB">$finfo</span><span style="color: #007700">, </span><span style="color: #0000BB">$filename</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/* close connection */<br /></span><span style="color: #0000BB">finfo_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$finfo</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> <p class="para">The above example will output:</p> <div class="example-contents screen"> <div class="cdata"><pre> text/plain; charset=us-ascii </pre></div> </div> </div> <div class="refsect1 seealso" id="refsect1-function.finfo-open-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.finfo-close.html" class="function" rel="rdfs-seeAlso">finfo_close()</a> - Close fileinfo resource</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.finfo-file.html">finfo_file</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.finfo-set-flags.html">finfo_set_flags</a></div> <div class="up"><a href="ref.fileinfo.html">Fileinfo Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>