diff --git a/FileBinaryMimeTypeGuesser.php b/FileBinaryMimeTypeGuesser.php index e00ce65..59e55f7 100644 --- a/FileBinaryMimeTypeGuesser.php +++ b/FileBinaryMimeTypeGuesser.php @@ -33,7 +33,7 @@ class FileBinaryMimeTypeGuesser implements MimeTypeGuesserInterface * * @param string $cmd The command to run to get the MIME type of a file */ - public function __construct(string $cmd = 'file -b --mime %s 2>/dev/null') + public function __construct(string $cmd = 'file -b --mime -- %s 2>/dev/null') { $this->cmd = $cmd; } @@ -76,7 +76,7 @@ public function guessMimeType(string $path): ?string ob_start(); // need to use --mime instead of -i. see #6641 - passthru(sprintf($this->cmd, escapeshellarg($path)), $return); + passthru(sprintf($this->cmd, escapeshellarg((0 === strpos($path, '-') ? './' : '').$path)), $return); if ($return > 0) { ob_end_clean(); diff --git a/Tests/AbstractMimeTypeGuesserTest.php b/Tests/AbstractMimeTypeGuesserTest.php index 3ac9382..70e419c 100644 --- a/Tests/AbstractMimeTypeGuesserTest.php +++ b/Tests/AbstractMimeTypeGuesserTest.php @@ -27,6 +27,21 @@ public static function tearDownAfterClass(): void abstract protected function getGuesser(): MimeTypeGuesserInterface; + public function testGuessWithLeadingDash() + { + if (!$this->getGuesser()->isGuesserSupported()) { + $this->markTestSkipped('Guesser is not supported'); + } + + $cwd = getcwd(); + chdir(__DIR__.'/Fixtures/mimetypes'); + try { + $this->assertEquals('image/gif', $this->getGuesser()->guessMimeType('-test')); + } finally { + chdir($cwd); + } + } + public function testGuessImageWithoutExtension() { if (!$this->getGuesser()->isGuesserSupported()) { diff --git a/Tests/Fixtures/mimetypes/-test b/Tests/Fixtures/mimetypes/-test new file mode 100644 index 0000000..b636f4b Binary files /dev/null and b/Tests/Fixtures/mimetypes/-test differ