Skip to main content
  1. Entry/

Chrome からファイルをアップロードするとファイルのタイプがとれない

/** * Guess the file mime type with PECL Fileinfo extension * * @param string $file The absolute path of a file * * @return string The mime type of the file (null if not guessable) */ protected function guessFromFileinfo($file) { if (!function_exists(finfo_open) || !is_readable($file)) { return null; }

<span class="synStatement">if</span> <span class="synSpecial">(</span><span class="synStatement">!$</span><span class="synIdentifier">finfo</span> <span class="synStatement">=</span> <span class="synPreProc">new</span> finfo<span class="synSpecial">(</span>FILEINFO_MIME<span class="synSpecial">))</span>
<span class="synSpecial">{</span>
  <span class="synStatement">return</span> <span class="synType">null</span>;
<span class="synSpecial">}</span>

<span class="synStatement">$</span><span class="synIdentifier">type</span> <span class="synStatement">=</span> <span class="synStatement">$</span><span class="synIdentifier">finfo</span><span class="synType">-&gt;</span>file<span class="synSpecial">(</span><span class="synStatement">$</span><span class="synIdentifier">file</span><span class="synSpecial">)</span>;

<span class="synComment">// remove charset (added as of PHP 5.3)</span>
<span class="synStatement">if</span> <span class="synSpecial">(</span><span class="synConstant">false</span> <span class="synStatement">!==</span> <span class="synStatement">$</span><span class="synIdentifier">pos</span> <span class="synStatement">=</span> <span class="synIdentifier">strpos</span><span class="synSpecial">(</span><span class="synStatement">$</span><span class="synIdentifier">type</span>, '<span class="synConstant">;</span>'<span class="synSpecial">))</span>
<span class="synSpecial">{</span>
  <span class="synStatement">$</span><span class="synIdentifier">type</span> <span class="synStatement">=</span> <span class="synIdentifier">substr</span><span class="synSpecial">(</span><span class="synStatement">$</span><span class="synIdentifier">type</span>, <span class="synConstant">0</span>, <span class="synStatement">$</span><span class="synIdentifier">pos</span><span class="synSpecial">)</span>;
<span class="synSpecial">}</span>

<span class="synStatement">return</span> <span class="synStatement">$</span><span class="synIdentifier">type</span>;

}

// …略…