4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpFoundation\File\MimeType;
14 use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
15 use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException;
18 * Guesses the mime type with the binary "file" (only available on *nix).
20 * @author Bernhard Schussek <bschussek@gmail.com>
22 class FileBinaryMimeTypeGuesser implements MimeTypeGuesserInterface
29 * The $cmd pattern must contain a "%s" string that will be replaced
30 * with the file name to guess.
32 * The command output must start with the mime type of the file.
34 * @param string $cmd The command to run to get the mime type of a file
36 public function __construct($cmd = 'file -b --mime %s 2>/dev/null')
42 * Returns whether this guesser is supported on the current OS.
46 public static function isSupported()
48 return '\\' !== DIRECTORY_SEPARATOR && function_exists('passthru') && function_exists('escapeshellarg');
54 public function guess($path)
56 if (!is_file($path)) {
57 throw new FileNotFoundException($path);
60 if (!is_readable($path)) {
61 throw new AccessDeniedException($path);
64 if (!self::isSupported()) {
70 // need to use --mime instead of -i. see #6641
71 passthru(sprintf($this->cmd, escapeshellarg($path)), $return);
78 $type = trim(ob_get_clean());
80 if (!preg_match('#^([a-z0-9\-]+/[a-z0-9\-\.]+)#i', $type, $match)) {
81 // it's not a type, but an error message