Current Location: Home> Latest Articles> Best Practices for Using finfo::set_flags with finfo_file()—Do You Know Them?

Best Practices for Using finfo::set_flags with finfo_file()—Do You Know Them?

gitbox 2025-09-12
<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-comment">// Some PHP pre-code unrelated to the article content</span></span><span>
</span><span><span class="hljs-title function_ invoke__">date_default_timezone_set</span></span><span>(</span><span><span class="hljs-string">&#039;Asia/Shanghai&#039;</span></span><span>);
</span><span><span class="hljs-variable">$timestamp</span></span><span> = </span><span><span class="hljs-title function_ invoke__">time</span></span><span>();
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"Current timestamp: <span class="hljs-subst">$timestamp</span></span></span><span>\n";
</span><span><span class="hljs-meta">?&gt;</span></span><span>
<p><hr></p>
<p></span><?php<br>
<span class="hljs-comment">/**</p>
<ul data-is-last-node="" data-is-only-node="">
<li>
<p>Title: Best Practices for Using finfo::set_flags with finfo_file()—Do You Know Them?</p>
</li>
<li></li>
<li>
<p>In PHP, the finfo class offers an efficient and reliable way to handle file type detection.</p>
</li>
<li>
<p>The combination of finfo::set_flags() and finfo_file() allows for more precise and flexible file type checking.</p>
</li>
<li></li>
<li>
<ol>
<li>
<p>finfo::set_flags() Function</p>
</li>
</ol>
</li>
<li>
<p>finfo::set_flags() is used to set flag values in the finfo object while performing file information checks.</p>
</li>
<li>
<p>These flags control the detail and handling of the returned result, such as:</p>
</li>
<li></li>
<li>
<ul>
<li>
<p>FILEINFO_MIME_TYPE: Returns only the MIME type.</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>FILEINFO_MIME: Returns complete MIME information, including charset.</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>FILEINFO_PRESERVE_ATIME: Does not modify the access time when checking files.</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>FILEINFO_RAW: Returns raw information without parsing.</p>
</li>
</ul>
</li>
<li></li>
<li>
<p>By setting appropriate flags, different requirements can be met for various scenarios.</p>
</li>
<li></li>
<li>
<ol start="2">
<li>
<p>finfo_file() Function</p>
</li>
</ol>
</li>
<li>
<p>finfo_file() is the core method used to detect the MIME type or other information of a specified file.</p>
</li>
<li>
<p>The usage is as follows:</p>
</li>
<li></li>
<li>
  • $finfo = new finfo();

  • $type = $finfo->file('example.jpg');

  • echo $type; // Output: image/jpeg

  • finfo_file() returns the corresponding file information based on the flags set in the finfo object.

    1. Best Practices

    1. Set Default Flags When Creating finfo Object

  • $finfo = new finfo(FILEINFO_MIME_TYPE);

  • This ensures that the returned value is always the MIME type, avoiding additional parsing later.

    1. Dynamically Modify Flags When Necessary

  • $finfo->set_flags(FILEINFO_MIME | FILEINFO_PRESERVE_ATIME);

  • For temporary needs, you can dynamically adjust the flags after creating the object without re-instantiating it.

    1. Use Exception Handling

  • try {

  • $finfo = new finfo(FILEINFO_MIME_TYPE);
    
  • $type = $finfo-&gt;file(&#039;example.jpg&#039;);
    
  • if ($type === false) {
    
  •     throw new Exception(&#039;Unable to identify file type&#039;);
    
  • }
    
  • echo "File type: $type\n";
    
  • } catch (Exception $e) {

  • echo &#039;Error: &#039; . $e-&gt;getMessage();
    
  • }

  • This ensures that the program can handle cases where the file is missing or has an abnormal format, without crashing.

    1. Avoid Recreating the Object

  • If you need to check a large number of files, it’s recommended to reuse the same finfo object to improve performance:

  • $finfo = new finfo(FILEINFO_MIME_TYPE);

  • $files = ['a.jpg', 'b.png', 'c.gif'];

  • foreach ($files as $file) {

  • echo $finfo-&gt;file($file) . "\n";
    
  • }

  • Conclusion:

    • Using finfo::set_flags() allows for flexible file detection output.

    • finfo_file() is the core method for retrieving file types.

    • Default flag settings when creating objects + reuse of objects + exception handling = best practices.

  • With these methods, you can retrieve file information more efficiently and safely in PHP.
    */
    ?>